Binary search

Find a number in a list sorted from smallest to largest. It works like finding a page in a book. Open it in the middle and the page number tells you which half to search next.

Let’s look in the middle. Is that our number? We found it! If our number is smaller than the middle one, we look to the left. If it’s larger, we look to the right.

We drop the half we can rule out and look in the middle of what’s left. We keep cutting the row in half until we find our number or run out of places to look.

Mathematicians call this logarithmic time and write it as O(log n).

In plain English, even with 1000 numbers, we only need to check the middle at most 10 times. Often we find our number sooner!