Binary Search Complexity: Why It Is (Not , , or )
Analysis of Binary Search Algorithm | Time complexity of Binary Search Algorithm | O(1) | O(log n)
Binary search (Binary search ) has time complexity that grows logarithmically with input size: in the average and worst cases.2 This makes the correct option among:
- (i)
- (ii)
- (iii)
- (iv)
✅ Correct answer: (ii) .2
Key idea: halving the search space
Each iteration of binary search compares the target to the middle element and then keeps only one half of the remaining interval. After iterations, the remaining number of candidates is: We stop when the interval size becomes 1: So the number of comparisons (and thus runtime) scales as . This matches standard complexity analyses of binary search.2
Footnotes
-
Time and Space Complexity Analysis of Binary Search Algorithm - GeeksforGeeks - Explicitly gives time complexities: best , average , worst . ↩ ↩2 ↩3 ↩4
-
Time and Space Complexity of Binary Search - Scaler Topics - States worst/average time complexity is and mentions the recurrence form . ↩ ↩2 ↩3
How Binary Search Shrinks Work
Initial interval
Step 0Search range has size ."
First comparison
Step 1Compare with middle; discard half → size ."
Second comparison
Step 2Discard half again → size ."
Termination
Step kWhen , then ."
Complexity derivation for binary search
- 1Step 1
Binary search works on a sorted array of length .
- 2Step 2
After one comparison, at most half the interval remains (size ).
- 3Step 3
Halving repeatedly gives remaining candidates .
- 4Step 4
Solve ⇒ .
- 5Step 5
Each iteration does work, so total time is (worst/average).2
Footnotes
-
Time and Space Complexity Analysis of Binary Search Algorithm - GeeksforGeeks - Explicitly gives time complexities: best , average , worst . ↩
-
Time and Space Complexity of Binary Search - Scaler Topics - States worst/average time complexity is and mentions the recurrence form . ↩
-
Pro Tip
Whenever an algorithm cuts the problem size by a constant factor each step (like halving), check whether the runtime resembles a logarithm: implies .
Footnotes
-
Time and Space Complexity Analysis of Binary Search Algorithm - GeeksforGeeks - Explicitly gives time complexities: best , average , worst . ↩
Common confusion
Binary search assumes the array is already sorted; otherwise you must pay the cost to sort first, and the overall complexity may become due to sorting, not because binary search itself is . (Sorting is a different step.)
Time complexity options vs. binary search
Binary search in a sorted array: correct choice is (worst/average).
Quick FAQ on the answer choices
Knowledge Check
What is the time complexity of binary search on a sorted array?
Explore Related Topics
Solving the Recurrence $T(n)=T(n-1)+n$ by Substitution Method
The course shows how to solve the decrease‑by‑one recurrence (T(n)=T(n-1)+n) (with (T(1)=1)) using the substitution method.
- Repeatedly substitute (T(n-i)=T(n-i-1)+(n-i)) until reaching the base case, yielding (T(n)=T(1)+2+3+\dots+n).
- The resulting sum is the triangular number (\frac{n(n+1)}{2}).
- The dominant term (\frac{1}{2}n^{2}) gives a tight asymptotic bound (\Theta(n^{2})).
- For recurrences of the form (T(n)=T(n-1)+f(n)), expanding to a summation quickly reveals the closed form.
Asymptotic Notation in Algorithm Analysis
Database Indexing Mechanics: B-Trees, LSM-Trees, and Sequential Scans