Computing

How to find one name among a billion in 30 looks: binary search

Rows of squares narrowing by half each row until one lime square remains, under the title A billion names, 30 looks
30seconds
The 30-second version

Pick a number from 1 to 1,000,000 and you can find it in at most 20 yes-or-no questions. The trick is binary search: look in the middle of a sorted list, throw away the half that cannot hold your target, and repeat. Every look halves the problem, so doubling the list adds only one extra step.

Play it yourself in a grid, then see why a one-line way of finding the middle hid a bug in famous code for years.

Deep dive · 4 min
Deep dive · 4 min

More in Computing

View all →