Searching and sorting · 15 min

Binary search: halve it until you find it

Why one comparison can rule out half of a sorted array, and how to never get the boundaries wrong.

Builds onLinear search

01Why it matters

Twenty guesses for a million names

Think of a number between 1 and 1,000,000 and let a friend guess. If you only say "yes" or "no", they could need a million guesses. If you say "higher" or "lower", they need at most 20, because every answer throws away half of what is left: a million, half a million, a quarter million… down to one in about 20 halvings.

Binary search is that game played on a sorted array. It is how a dictionary lookup, a database index or git bisect finds things fast. The catch is the word sorted: the "higher or lower" answer only means something when the order is fixed.

The number of halvings it takes to shrink nn items to one is log⁡2n\log_2 n, so binary search needs at most ⌈log⁡2(n+1)⌉\lceil \log_2 (n+1) \rceil probes, against up to nn for checking every element.

02Key parts

The pieces you will see

lo and hi
The ends of the window: the positions that could still hold the target. Everything outside has been ruled out.
mid
The middle of the window, ⌊(lo + hi) / 2⌋. The only element the algorithm actually looks at each step.
probe
One comparison of A[mid] with the target. Counting probes is how we measure the work.
sorted order
The promise that makes the trick legal: everything left of mid is no bigger, everything right is no smaller.

03See it work

Watch the window halve

Step forward with the arrow buttons (or click the visualiser and use ← →). Watch the shaded window: each probe throws away one half, and the narration says why that half cannot contain the target.

15 sorted values: the target is found within 4 probes, since 2⁴ = 16 > 15.

04Predict

Your turn: predict each move

This time the player stops before every decision and asks what happens next. Decide from the values you can see, then step forward to check. Predicting before you watch is what turns watching into learning.

Make 4 predictions: step forward and answer each question before the next step plays.

0 of 4 made

05Try it

Break it on purpose

Try the inputs that sit at the edges: a target at the very first or last position, or one that isn't there at all. Count the probes each time and compare them with ⌈log⁡2(n+1)⌉\lceil \log_2 (n+1) \rceil. Then type your own sorted values.

Change the input below: try a target that is missing, or the largest array the visualiser allows.

06Check yourself

Check yourself

  1. Question 1 of 5

    At most how many probes does binary search need for 1,000 sorted items?

    Your answer comes back in Review in a few days, so it sticks.

  2. Question 2 of 5

    A = [3, 8, 15, 22, 31, 47, 60] and the target is 31. The first probe looks at A[3] = 22. What happens next?

  3. Question 3 of 5

    Which update keeps the invariant when A[mid] < target?

  4. Question 4 of 5

    lo = 4, hi = 4 and A[4] is not the target. What does the next step do?

  5. Question 5 of 5

    Is binary search guaranteed to find 4 in [5, 1, 4, 2, 3]?

07Explain it back

Say it in your own words

Why may binary search throw away half the array after a single comparison, and why does it always stop?

08Recap

What to remember

  • Binary search keeps a window that must contain the target if it exists, and halves it with each probe.
  • It needs about log⁡2n\log_2 n probes: 20 for a million items.
  • Every branch must remove mid, or the window can stop shrinking.
  • It only works on sorted data. The same idea powers searching over answers too: whenever "too small / too big" is monotonic, you can binary search it.
Back to path