all lessons

algorithms

did you know a million items can take just twenty checks?

every good question throws away half the possibilities.

the idea at a glance

1,000,000 → 20

sorted entries → at most twenty checks

compare with the middle, then keep only the half that could contain the target.

  1. compare
  2. discard half
  3. repeat

the idea

in a sorted list of a million entries, binary search can find a target, or establish that it is absent, in at most twenty midpoint checks. the trick is refusing to inspect entries one by one.

how it works

a million possibilities become about 500,000, then 250,000, then 125,000. after ten halvings, only about a thousand remain. ten more finish the job.

go deeper

what makes a question useful?

in a sorted list of a million entries, binary search can find a target, or establish that it is absent, in at most twenty midpoint checks. the trick is refusing to inspect entries one by one.

look at the middle entry. if the target is smaller, discard everything above it; if larger, discard everything below it. sorting makes each comparison informative about an entire half of the list.

keep cutting the remaining problem in half

a million possibilities become about 500,000, then 250,000, then 125,000. after ten halvings, only about a thousand remain. ten more finish the job.

this is the same strategy as guessing a number when someone reliably answers higher or lower. a guess in the middle keeps the worst-case remaining interval as small as possible.

why only twenty?

two raised to the twentieth power is 1,048,576. twenty binary decisions can distinguish more than a million possibilities. a standard midpoint search on one million sorted entries needs no more than twenty comparisons.

2²⁰ = 1,048,576; search work grows roughly as log₂(n)

where is the catch?
  • the entries must be sorted according to the comparison you use. sorting an unsorted collection is separate work.
  • the speed advantage also assumes you can jump cheaply to the middle. walking a linked list to reach that entry changes the cost.
sources & further reading

concepts: binary search · logarithms · algorithmic complexity

2 minute read