Skip to the workspace
Pebellslearn by building
Asymptotic Notation  ›  Lesson

Logarithmic Time — The Power of Halving

Logarithmic 12 minAn algorithm is logarithmic when each step cuts the remaining problem by a constant fraction, usually half, so doubling the input adds only one more step.

computer science / Logarithmic Time — The Power of Halving

Explore each scene at your own pace.

01 · The rule

The rule

Logarithmic Time — The Power of Halving

Look up a word in a paper dictionary and you never read page by page. You open near the middle, see you have gone too far, and throw away half the book. A few halvings later you are on the right page. A dictionary of 1000 pages needs about 10 such halvings; one of 2000 pages needs about 11. That gentle growth is logarithmic time.

The rule

Halve the problem each step; doubling the input adds just one step.

If every step throws away half of what is left, the number of steps is the number of times you can halve n before reaching 1. That number is the base-2 logarithm of n, written log₂ n, and the growth rate is written O(log n).

The powers of two make it easy to feel:

n halvings to reach 1
8 3
1,024 10
about 1 million about 20
about 1 billion about 30

Going from a thousand to a billion items multiplies n by a million, yet the steps only go from 10 to 30. Binary search on a sorted list is the classic example: compare with the middle item, keep the half that could still hold the target, repeat.

Halving needs a reason to throw half away. Binary search only works because the list is sorted; on an unsorted list, the middle item tells you nothing about where the target is.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.