An exploration of the binary search algorithm, showing how divide-and-conquer strategy achieves logarithmic time complexity by repeatedly halving the search space.
(a) Precondition
The array must be sorted in ascending order to allow elimination of elements.
(b) Setup
Initialize pointers low at 0 and high at n-1.
(c) Iterate
Calculate midpoint and compare; update pointers based on target value relative to middle element.
(d) Termination
If target is found return index, else if pointers cross return not found.
(e) Complexity
Since each step halves the remaining array, the growth is logarithmic.
Original question
Explain how the binary search algorithm works on a sorted array, and state its time complexity in big-O notation.
Ask me anything about this lesson. I'll answer using what we just covered.