Homework problem C (corrected)

Problem C

Assigned Sept 11, due Sept 25, accepted until Sept 30.

This problem is based on recurrences chapter 4, specifically on problem 4.3a.

A recursive binary search:

template<class Ptr>
Ptr binsearch(Ptr p, Ptr b, Ptr e)
/* Pre:
p points to desired item (somewhere in mem). 
b and  e define the sorted range of mem in which to search.
b < e is required.
We assume standard comparison operators can be used on the 
datatype pointed to by Ptr instances.
   Post:
Return ptr to occurrence of *p in sorted array *[b..e).
Return NULL if no such value exists.
*/
{   if (e - b == 1) 
	if (*p == *b return b; // successful search
	else return NULL; // failed search
    else
    {
	Ptr m = b + (e - b)/2;
	if (*p < *m) return binsearch(p, b, m); 
	else return binsearch(p, m, e);
    }
}

Professor Stu Bjorn prefers the old method of array and index parameters instead of pointers. Stu writes this equivalent code:

template<class V>
size_t binsearch(V& x, V A[], size_t b, size_t e)
/* Pre:
x is the desired item. A is a sorted array (using 1-based indexing). 
b and e define the sorted range in which to search.
b < e is required.
We assume standard comparison operators can be used on datatype V.
   Post:
Return location of occurrence of x in sorted array [A[b]..A[e]).
Return 0 if no such value exists (a not valid array index).
*/
{   if (e - b == 1) 
	if (x == A[b]) return b; // successful search
	else return 0; // failed search
    else
    {
	size_t m = (b + e)/2;
	if (x < A[m]) return binsearch(x, A, b, m) ;
	else return binsearch(x, A, m, e);
}
  1. (3 points) Suppose arrays are passed as pointers (as is done in C++). State a recurrence relation for the time taken by this binsearch, and solve it.

  2. (3 points) Suppose arrays are passed by value (i.e. entire arraye copied) on each call to binsearch, including the recursive calls. State a recurrence relation for the time taken by this binsearch including the time to copy data, and solve it. Assume unit cost to copy one value.

  3. (3 points) Suppose arrays are passed by value (i.e. are copied) on each call to binsearch, including the recursive calls. However the smart compiler is able to see that only the region between index b and e will be accessed. The region is inclusive of A[b], exclusive of A[e], so that only that subarray consisting of e - b elements is copied. State a recurrence relation for the time taken by this binsearch including the time to copy data, and solve it.

  4. (1 point) Student Imso Sharp notices that the binsearches on the original problem sheet require an index or pointer comparison and two value comparisons, an `==' and a `<', per step. The average depth of recursion for a successful search is about lg(n), so the average number of comparisons is about 2lg(n). Rewrite the Ptr based binsearch so that the average successful search uses about lg(n) comparisions. Hint: only one value comparison need be used in the typical step.

    To say f(n) is "about" g(n) means that f(n)/g(n) --> 1, as n --> infinity.