22C:44 Algorithms
Due Tuesday, April 2, 2002
Assume that i £ j, A[i-1] ³ A[i], and A[j] £ A[j+1]. These assumptions guarantee that A[i..j] contains a local minimum.
/* Assume A[i-1] >= A[i] and A[j] <= A[j+1]. Return index of local minimum */
int FindLocalMinimum(int A[], int i, int j)
{
...
return(index-of-local-minimum);
}
NOTE: in this parts b and c below, express your answers in terms of n,
where n is defined to be equal to j-i+1 (the number of elements covered
in a call FindLocalMinimum(A,i,j)).
b. Write a recurrence equation for FindLocalMinimum.
c. (3 points) Give a tight bound on the running time of FindLocalMinimum.
/* Given array A, and integers left and right, where left <= right,
SILLYSORT(A,left,right) sorts the items in A[left..right].
NOTE: this is not a good way to sort; it is logically sound but
a bit silly.
*/
void SILLYSORT(int A[] ; int left, int right);
int middle;
if (l != r) {
middle = (left + right)/2;
SILLYSORT(A,left,middle);
SILLYSORT(A,middle+1,right);
if (A[left] > A[middle+1]) then
swap(A[left],A[middle+1]);
SILLYSORT(?,?,?);
}