Halving: O(log n) and O(n log n)
🌍 In real life. A friend thinks of a number from 1 to 100. You guess 50; they say "higher". You guess 75; "lower". Each guess throws away half of what is left, so you never need more than 7 guesses for 100 numbers, and only 20 for a million. The same trick is how you find a word in a dictionary: open the middle, and keep halving.
Work that halves the problem at every step hardly grows at all.
The idea in one sentence: log₂ n is how many times you can halve n before reaching 1, and an algorithm that halves its work each step is O(log n).
Its words:
- log₂ n: how many times n can be halved before it reaches 1; log₂ 1,024 = 10
- O(log n): work that grows by just one step each time n doubles, like binary search
- O(n log n): O(log n) work for each of n items, or log n rounds of O(n) work, like a good sort
How it works, step by step: start with n, and repeat n //= 2 until n is 1, counting the rounds. 16 → 8 → 4 → 2 → 1 is 4 rounds, so log₂ 16 = 4. Watch the bar halve, and the counter only creep up:
Why it grows so slowly. Doubling n adds just one more halving: 16 takes 4 rounds, 32 takes 5, 64 takes 6. Going from a thousand to a million (a thousand times more) only adds about 10 rounds.
O(n log n) means doing O(log n) work for each of the n items. It is the cost of the best general sorting methods (World 7), a little more than O(n) but far less than O(n²).
Now see all five shapes side by side. Each tower is the number of steps for the current n; watch O(n²) shoot off the chart while O(log n) barely moves:
def guesses_needed(n):
# the worst case of the guessing game for numbers 1..n
low, high, guesses = 1, n, 0
while low <= high:
guesses += 1
middle = (low + high) // 2
low = middle + 1 # pretend the answer is always "higher"
return guesses
for n in [100, 1000, 1_000_000]:
print(n, guesses_needed(n))Mistakes and edge cases. Halve with n //= 2, not n /= 2: / gives floats (8 → 4.0 → 2.0 → 1.0 → 0.5 …), and a float can keep halving past 1 for about a thousand rounds. Stop at while n > 1: while n > 0 counts one round too many (16 would give 5, not 4). And n = 1 needs 0 halvings.
Quiz
What does this print?
n = 64 rounds = 0 while n > 1: n //= 2 rounds += 1 print(rounds)An O(log n) algorithm takes 10 steps for n = 1024. About how many for n = 2048?
- 11
- 20
- 40
Which grows fastest as n gets big?
- O(n²)
- O(n log n)
- O(log n)
What does this print?
steps = 0 for item in range(8): n = 8 while n > 1: n //= 2 steps += 1 print(steps)Why is the guessing game O(log n)?
- Each guess throws away half of the numbers still possible
- It tries every number once
- It always guesses right the first time
This should count how many times 16 can be halved before it reaches 1 (4 times), but it never stops. Which line is wrong?
n = 16 rounds = 0 while n > 1: n / 2 rounds += 1 print(rounds)
Coding tasks
Write
halvings(n): how many timesncan be halved with//= 2before it reaches 1 (for n of 1 or less, 0).Play the guessing game: write
guess(secret, n)that findssecretin 1..n by always guessing the middle of what is left, and gives back how many guesses it took.