Skip to the workspace
Pebellslearn by building
Recursion, Searching & Sorting  ›  Lesson

Binary Search & Library Search

Binary Search 22 minFind anything in sorted data by halving the range every step

dsa / Binary Search & Library Search

Explore each scene at your own pace.

01 · Find a title in a 50,000-track library

Find a title in a 50,000-track library

Binary Search & Library Search

Definition
Binary search finds a target in a sorted list by checking the middle item and discarding the half that cannot contain the target, repeating until it is found or nothing is left.
In plain words
It is how you find a word in a dictionary: open near the middle, see whether your word comes before or after, and ignore the other half every time.

In short: scanning a list front to back (linear search) checks every item and costs O(n) — it works on anything, sorted or not. Binary search needs the data sorted, but then it opens the middle, decides which half the answer must be in from one comparison, and throws the other half away: O(log n). The whole thing rests on one invariant you must keep true every step — if the target is anywhere, it's between lo and hi.

A phone book opened in the middle, with the left half under a big X and an arrow pointing into the right half, repeated smaller twice more until one page remains circled

Find a title in a 50,000-track library

python

Each look halves what's left, so the number of looks is about log₂ n — double the library and you pay one more look, not thousands more:

What does this print?
n, looks = 1_000_000, 0
while n > 0:
    n //= 2
    looks += 1
print(looks)

Invariant: if title is anywhere in the list, it is at an index in lo..hi (both ends included). Start lo = 0, hi = len - 1; a mismatch to the left of mid sets lo = mid + 1, a mismatch to the right sets hi = mid - 1; when lo > hi the range is empty and the title isn't there.

step through it
1titles = ["Aria", "Blue", "Cove", "Dawn", "Echo"]
2lo, hi = 0, len(titles) - 1
3found = -1
4while lo <= hi:
5 mid = (lo + hi) // 2
6 if titles[mid] == "Dawn":
7 found = mid
8 break
9 if titles[mid] < "Dawn":
10 lo = mid + 1
11 else:
12 hi = mid - 1
13print(found)

Watch lo and hi close in on "Dawn" step by step. Now the same invariant on a bigger shelf, with every look drawn:

watch it move · array

Searching for "Glow": every look throws away half of what's left, so eight titles need only three looks.

The whole list is in play, so if "Glow" is anywhere it is between lo and hi. 8 cells. lo at index 0. hi at index 7.

1 / 6

The mistake people really make: getting the loop condition and the move off by one. This version looks right, and fails on the very last title:

broken — fix it

This should print 2, the index of "Cove". It prints -1 — the search gives up while one title is still unchecked.

Scene 1 of 8
Project context and further resources
You're building a piece ofTunebox — search & charts
This piece — find_track(): Finds a title in a sorted library in a handful of steps.
Scenario The library view keeps every title sorted A→Z, so typing a name doesn't scan 50,000 songs — binary search opens the middle and halves the range, finding any track in about 16 looks.
find_track.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
find_track(sorted_titles, title) → intThe index of the title, found by binary search — or -1 if it's missing.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.