← All posts
November 26, 2026 · 2 min read

Binary Search in Python: Halving a Phone Book (LeetCode 704)

Binary search finds a value in a sorted list by halving what's left each look: 1,000 pages in at most 10 looks. LeetCode 704 in Python.

Binary search finds a value in a sorted list by looking at the middle and throwing away the half the value can't be in. It is LeetCode 704: given a sorted list nums and a target, return the target's index, or -1 if it is not there.

A closed phone book drawn in cut paper under the line find 1 name in 1,000 pages

How do you find a name in a phone book?

Nobody reads a phone book from page one. The names are sorted, so you open it in the middle, see whether the name you want comes before or after that page, and tear away the half it can't be in. Then you do the same with the half that's left.

The left half of the phone book torn away, the right half left, and the readout look 1: 500 pages left

Why are ten looks enough for a thousand pages?

Each look either finds the page or halves what's left. If the look misses, the bigger of the two halves has at most half the pages. Starting from 1,000 pages, the worst case goes like this:

Look Pages left before the look
1 1,000
2 500
3 250
4 125
5 62
6 31
7 15
8 7
9 3
10 1

By the tenth look one page is left, and that is the one. A page-by-page search could need all 1,000 looks.

What does it look like in Python?

Two indexes, lo and hi, mark the stretch of the list still left. While any of it is left (lo <= hi), look at the middle index. If the value there is the target, return its index. If it's too small, the target can only be to the right, so lo jumps past mid. If it's too big, hi drops below mid. When lo passes hi, nothing is left and the target isn't there: return -1.

This is the program from the video, line for line:

def search(nums, target):
    lo, hi = 0, len(nums) - 1
    while lo <= hi:
        mid = (lo + hi) // 2
        if nums[mid] == target:
            return mid
        if nums[mid] < target:
            lo = mid + 1
        else:
            hi = mid - 1
    return -1

nums = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
print(search(nums, 23))

Output:

5

Which numbers does it look at?

Searching for 23 in the ten sorted numbers takes three looks:

Look lo hi mid nums[mid] Then
1 0 9 4 16 too small: lo = 5
2 5 9 7 56 too big: hi = 6
3 5 6 5 23 found: return 5

The sorted list 2 to 91 with 16 at the middle marked, the left half 2, 5, 8, 12, 16 greyed out, lo moved to index 5, and the readout look 1: 16 < 23

The first look throws away five numbers at once, the second three more, and the third lands on 23 at index 5. A scan from the left would check 2, 5, 8, 12, 16 and 23: six looks.

Only 23 and 38 left, 23 at index 5 in berry with lo and mid under it, the readout return 5 and the line return mid highlighted in the code

We also checked search against a plain scan on 5,000 random sorted lists, with targets that are in the list and targets that aren't, and the answers always agree. LeetCode's two examples, search([-1, 0, 3, 5, 9, 12], 9) and search([-1, 0, 3, 5, 9, 12], 2), return 4 and -1.

How fast is it?

Every look halves what's left, so the number of looks grows with the number of times you can halve n before reaching one: O(log n) time, and O(1) extra memory for the three indexes. The catch is the word sorted. On an unsorted list, throwing away a half could throw away the answer.

The readout halving: O(log n) above the finished binary search program

Try it

The phone book now has a million pages. How many looks does binary search need, at most, to find any one page? Halve a million until one page is left, then count the looks.