Binary Search Complexity: Why It Is O(logn)O(\log n) (Not O(n)O(n), O(n2)O(n^2), or O(nlogn)O(n\log n))

Binary Search Complexity: Why It Is O(logn)O(\log n) (Not O(n)O(n), O(n2)O(n^2), or O(nlogn)O(n\log n))

Verified Sources
Sep 12, 2026

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: O(logn)O(\log n) in the average and worst cases.2 This makes the correct option among:

  • (i) O(n)O(n)
  • (ii) O(logn)O(\log n)
  • (iii) O(n2)O(n^2)
  • (iv) O(nlogn)O(n\log n)

Correct answer: (ii) O(logn)O(\log n).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 kk iterations, the remaining number of candidates is: n2k\frac{n}{2^k} We stop when the interval size becomes 1: n2k=12k=nk=log2(n)\frac{n}{2^k} = 1 \Rightarrow 2^k = n \Rightarrow k = \log_2(n) So the number of comparisons (and thus runtime) scales as O(logn)O(\log n). This matches standard complexity analyses of binary search.2

Footnotes

  1. Time and Space Complexity Analysis of Binary Search Algorithm - GeeksforGeeks - Explicitly gives time complexities: best O(1)O(1), average O(logn)O(\\log n), worst O(logn)O(\\log n). 2 3 4

  2. Time and Space Complexity of Binary Search - Scaler Topics - States worst/average time complexity is O(logn)O(\\log n) and mentions the recurrence form T(n)=T(n/2)+1T(n)=T(n/2)+1. 2 3

How Binary Search Shrinks Work

Initial interval

Step 0

Search range has size nn."

First comparison

Step 1

Compare with middle; discard half → size n/2n/2."

Second comparison

Step 2

Discard half again → size n/4n/4."

Termination

Step k

When n/2k=1n/2^k = 1, then k=log2(n)k=\log_2(n)."

  1. 1
    Step 1

    Binary search works on a sorted array of length nn.

  2. 2
    Step 2

    After one comparison, at most half the interval remains (size n/2n/2).

  3. 3
    Step 3

    Halving repeatedly gives remaining candidates n2k\frac{n}{2^k}.

  4. 4
    Step 4

    Solve n2k=1\frac{n}{2^k}=1k=log2(n)k=\log_2(n).

  5. 5
    Step 5

    Each iteration does O(1)O(1) work, so total time is O(logn)O(\log n) (worst/average).2

    Footnotes

    1. Time and Space Complexity Analysis of Binary Search Algorithm - GeeksforGeeks - Explicitly gives time complexities: best O(1)O(1), average O(logn)O(\\log n), worst O(logn)O(\\log n).

    2. Time and Space Complexity of Binary Search - Scaler Topics - States worst/average time complexity is O(logn)O(\\log n) and mentions the recurrence form T(n)=T(n/2)+1T(n)=T(n/2)+1.

Pro Tip

Whenever an algorithm cuts the problem size by a constant factor each step (like halving), check whether the runtime resembles a logarithm: nn/2n/4n \to n/2 \to n/4 \to \dots implies O(logn)O(\log n).

Footnotes

  1. Time and Space Complexity Analysis of Binary Search Algorithm - GeeksforGeeks - Explicitly gives time complexities: best O(1)O(1), average O(logn)O(\\log n), worst O(logn)O(\\log n).

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 O(nlogn)O(n\log n) due to sorting, not because binary search itself is O(nlogn)O(n\log n). (Sorting is a different step.)

Quick FAQ on the answer choices

Knowledge Check

Question 1 of 3
Q1Single choice

What is the time complexity of binary search on a sorted array?