A girl and a robot thinking together

Think together · Start with a little story

The robot halves a set of 16 possible positions. The girl sees that after just a few steps, very few candidates remain.

Lay out the process

Follow the steps, then explain them in your own words
  1. 16 candidatesReduce to about 8
  2. 8 candidatesReduce to about 4
  3. 4 candidatesReduce to about 2
  4. 2 candidatesReduce to about 1

Halving the candidate set each time means the number of steps grows slowly, logarithmically.

If the data size doubles, binary search usually needs only one more halving step. This diagram shows how the candidate set changes, not the exact comparisons of a particular implementation.

How can each step rule out half?

Find 7 among cards sorted from smallest to largest. Gray cards have been ruled out.

Step 1 · Check the middle index, 2
13579

The middle value, 5, is less than 7. It and everything to its left cannot be the target.

Step 2 · Keep the two candidates on the right
×××79

Using the lower middle index, compare the value at index 3, which is 7.

Step 3 · Find the target
×××7 ✓9

Return index 3, the fourth card. There is no need to check 9.

01What does binary search do each time?

It does more than check one element. More importantly, each comparison lets it rule out about half of the candidates.

n→n/2→n/4→n/8...

02How many halvings does it take for 1,024 elements?

1,024→512→256→ ... →1

Because:

1,024 = 210

It takes about 10 halvings.

03What does log₂n tell us?

You can think of log₂ n as asking:

How many times must you multiply 2 by itself to reach n?

Equivalently: about how many times must you keep dividing n by 2 to get down to 1?

04Why write O(log n) instead of O(log₂ n)?

Logarithms with different bases differ only by a constant factor:

log₂ n = a constant × log₁₀ n

Asymptotic Big O analysis ignores fixed constant factors, so we usually write O(log n).

05How slowly does O(log n) grow?

nApproximate log₂ n
1,02410
1,048,57620
About 1 billion30

The input can grow many times larger while the number of operations increases only a little.

06Does halving every time always mean O(log n)?

If each round does O(1) extra work and the problem size shrinks by a fixed ratio each round, the common result is indeed O(log n).

If each round also does a lot of work internally, include that computation when analyzing the total complexity.

What have you learned?

  • Binary search roughly halves its candidate range in every round.
  • Repeatedly halving n down to 1 takes about log₂ n rounds.
  • Therefore, binary search takes O(log n) time.
  • Big O notation usually leaves out the logarithm’s base.
  • O(log n) grows very slowly.

Next: How Can You Make a Slow Program Faster?

Your turn to try

If 16 candidates become 32, does the number of halvings roughly double?

Ready? Open to see the explanation

No. It usually adds only one round: first reduce 32 to about 16, then follow the original process.