Suppose that we have a sorted array of integers $a[0],...,a[n]$ such that
$$a[i] \le a[j] \text{ for } 0 \le i \le j \le n$$
A student designs the following algorithm that searches for an occurrence of an integer b in the sequence $a[0],...,a[n]$:
boolean search(int[] a, first, last, int b)
{
int middle = (first+last)/2;
if (middle == first && a[middle]!=b)
return false;
if (a[middle] == b)
return true;
if (a[middle] < b)
return search(a, middle, last, b);
else
return search(a, first, middle, b);
}
Develop a recurrence formula for the number of steps that need to be executed to complete the algorithm. For simplicity, we count all comparisons on lines 1 to 3 and the computation on line 0 as a single step.
My solution:
$$T(n) = T(n/2) + O(1)$$
I believe this algorithm is related to Binary Search Algorithm which is able to search for an element in a sorted list.
However, I am cheating here and not really trying to break-down the code in a way which helps me calculate a recurrence. Frankly am not sure where to being. Any help/hints are greatly appreciated.