Referring back to the searching problem (see Exercise 2.1-3), observe that if the sequence $A$ is sorted, we can check the midpoint of the sequence against $v$ and eliminate half of the sequence from further consideration. Binary search is an algorithm that repeats this procedure, halving the size of the remaining portion of the sequence each time. Write pseudocode, either iterative or recursive, for binary search. Argue that the worst-case running time of binary search is $\Theta(\lg n)$.

Pseudo-code for ITERATIVE-BINARY_SEARCH(A, v)

Pseudo-code for RECURSIVE-BINARY-SEARCH(A, v, low, high)

Intuitively, in worst case, i.e. when $v$ is not at all present in $A$, we need to binary search over the whole array to return NIL. So in worst-case, we need to repeatedly divide the array by 2. So the running time is nothing but how many times the input size can be divided by 2, i.e. $\lg n$.

And using recurrence formula, we can say running time $T(n) = T((n - 1)/2) + \Theta(1) = \Theta(\lg n)$.

If you have any question or suggestion or you have found any error in this solution, please leave a comment below.