Understanding Binary Search: Theory & Mathematical Complexity
Binary search is an efficient divide-and-conquer algorithm used to find the position of a target value within a sorted array. Unlike linear search which examines every element sequentially, binary search repeatedly halves the search space.
Mathematical Time Complexity Derivation
Suppose an array has size $N$. After 1 step, the remaining elements are $N / 2$. After 2 steps, $N / 4$, and after $k$ steps, the search space reduces to $N / 2^k$. The algorithm terminates when the search space reduces to 1 element:
N / 2^k = 1 ==> 2^k = N ==> k = log2(N)
- Best-Case Time Complexity: $O(1)$ when the target is at the initial midpoint.
- Worst-Case Time Complexity: $O(\log n)$ when target is at the boundary or missing.
- Auxiliary Space Complexity: $O(1)$ iterative memory space.