Binary Search Complexity: Why the Correct Choice is
Binary search works on a sorted array by repeatedly comparing the target to the middle element and discarding half of the remaining search interval each step. This halving implies that the number of iterations (and thus the running time in the worst case) grows proportionally to a logarithm in , giving time complexity .2
Answer to the multiple-choice question: (ii) .
Key notions to keep straight: binary search; time complexity; worst-case; logarithmic.
Footnotes
-
Binary search (Wikipedia) - States classical binary search performs iterations in the worst case and describes constant work per iteration. ↩
-
Time and Space Complexity Analysis of Binary Search Algorithm (GeeksforGeeks) - Explicitly states binary search time complexity is by halving each step; also discusses best/average/worst. ↩
Why Binary Search is O(log n)
Deriving the $O(\log n)$ time complexity for binary search
- 1Step 1
At the beginning, the candidate search range has elements.
- 2Step 2
After comparing to the middle element, binary search keeps only one half, so the interval size becomes , then , etc.2
Footnotes
-
Binary search (Wikipedia) - States classical binary search performs iterations in the worst case and describes constant work per iteration. ↩
-
Time and Space Complexity Analysis of Binary Search Algorithm (GeeksforGeeks) - Explicitly states binary search time complexity is by halving each step; also discusses best/average/worst. ↩
-
- 3Step 3
After iterations the interval size is . Binary search stops when .
- 4Step 4
Setting gives , so , hence the number of steps is and the time is .2
Footnotes
-
Binary search (Wikipedia) - States classical binary search performs iterations in the worst case and describes constant work per iteration. ↩
-
Binary Search Algorithm (EnjoyAlgorithms) - Derives and concludes , matching the halving argument. ↩
-
- 5Step 5
Each iteration performs a constant amount of work (computing the mid index and a comparison), so total time stays .2
Footnotes
-
Binary search (Wikipedia) - States classical binary search performs iterations in the worst case and describes constant work per iteration. ↩
-
Time and Space Complexity Analysis of Binary Search Algorithm (GeeksforGeeks) - Explicitly states binary search time complexity is by halving each step; also discusses best/average/worst. ↩
-
Matching the options
Because binary search’s interval size shrinks by a factor of 2 each step, the worst-case number of iterations is logarithmic in .2 Therefore:
- (i) — incorrect (that would correspond to checking one element per step, like linear search).
- (ii) — correct.
- (iii) — incorrect (quadratic time would require something like nested loops over ).
- (iv) — incorrect (that growth typically comes from combining linear work across levels, e.g., merge sort, not from halving a single search interval).
To contrast, linear search has worst-case time , while binary search on sorted data has time .
Footnotes
-
Binary search (Wikipedia) - States classical binary search performs iterations in the worst case and describes constant work per iteration. ↩
-
Time and Space Complexity Analysis of Binary Search Algorithm (GeeksforGeeks) - Explicitly states binary search time complexity is by halving each step; also discusses best/average/worst. ↩
-
Linear Search vs Binary Search (GeeksforGeeks) - Compares complexities: linear search vs binary search and notes sorted requirement. ↩ ↩2
Time complexity growth (intuition)
Conceptual comparison of common Big-O rates (not measured runtime).
Common pitfalls & clarifications
Pro Tip
When you see an algorithm that repeatedly halves the input size, immediately consider complexity—then verify that each iteration does only constant work.
Warning
Don’t confuse binary search with algorithms that divide and solve subproblems recursively without halving the search interval. Binary search halves the interval and follows only one branch, so it stays ; many divide-and-conquer algorithms branch into more work and can become .
Binary search lifecycle in one run
Initial interval
Step 1Search range size: ."
After first comparison
Step 2Range size reduces to ."
After $k$ comparisons
Step 3Range size is ."
Termination
FinalStop when range size reaches 1 → → time ."
Knowledge Check
The complexity of binary search (worst case) is:
Explore Related Topics
The Minimum Number of Colors for a Graph with $n>3$ Vertices and 2 Edges
A graph with more than three vertices that contains exactly two edges always has chromatic number .
- Because an edge exists, at least two colors are required: .
- With only two edges the graph is bipartite (either two disjoint or a plus isolated vertices), so two colors are sufficient: .
- No triangle or odd cycle can appear, eliminating the need for three or more colors.
- Isolated vertices are adjacent to none and can reuse any existing color, leaving the chromatic number unchanged.
- Consequently , making option (i) the correct answer.
Merge Sort: Working, Example, and Complexity Analysis
Asymptotic Notation in Algorithm Analysis