T
timorrill
I am working on a Recursive binary search algorithm (which probably
still needs some tweaking), but I am interested in counting the levels
of recursion encountered while searching for a particular element in a
sorted array.
Here's what I have for the method:
static int binsearch(int key, int[] x, int i, int j)
{
int mid;
if (i > j)
return i;
mid = (i+j)/2;
if (key <= x[mid])
return binsearch(key, x, i, mid-1);
else if (key > x[mid])
return binsearch(key, x, mid+1, j);
else
return mid;
}
What is the best way to introduce this counter?
still needs some tweaking), but I am interested in counting the levels
of recursion encountered while searching for a particular element in a
sorted array.
Here's what I have for the method:
static int binsearch(int key, int[] x, int i, int j)
{
int mid;
if (i > j)
return i;
mid = (i+j)/2;
if (key <= x[mid])
return binsearch(key, x, i, mid-1);
else if (key > x[mid])
return binsearch(key, x, mid+1, j);
else
return mid;
}
What is the best way to introduce this counter?