cadence_learn
think_like_a_programmer / level 06 of 12 ▶ 13 min

correct is not the finish line.

Two programs can both be right and be nothing alike. Race them, watch the gap, and meet the question that turns programming into computer science.

after this level you'll be able to
  • Explain why two correct programs can be wildly different in value
  • Describe binary search and the one thing it demands in return
  • Answer “what happens when the input gets ten times bigger?” for a simple method
the whole idea

correct is not the finish line

the one sentence

Two programs can both give the right answer and still be nothing alike — one finishes before you look up, the other is still going tomorrow.

why the gap is not small
LIST SIZE → LOOKS 32 1,000 1,000,000 8 billion one at a time — off the top of the chart — halving 6 10 20 33 Every time the list DOUBLES, halving needs exactly one more look. That is not a faster version of the same thing — it is a different kind of thing.
This picture is the reason algorithms are a subject. Both methods are correct on every list. Only one of them still works when the list gets real.

Up to now every level has been about getting it right. This one is about the question that comes immediately after, and it is the question that makes computer science a subject rather than a set of tricks: of the many correct ways, which one is good?

You already know this instinctively. Looking for a name in a phone book, you do not start at A and read every entry. You open it in the middle. Both work. One of them you would actually do.

try it yourself

race them

Below are two copies of the same sorted list of 32 numbers, and one number to find. The top lane checks every box from the left. The bottom lane jumps to the middle and throws half the list away, over and over.

▤ lab 06 · the race

Pick a number, press go, and count the boxes each one has to look at.

one at a time — start at the left, keep goinglooks: 0
halving — jump to the middle, throw half awaylooks: 0
Pick any number from 1 to 32 and press go. Watch how many boxes each one has to open.
the catch, and it is a real one

Halving only works because the list is sorted. On a jumbled list, throwing half away is nonsense — the number could be in the half you binned. This is the trade you will meet everywhere this semester: a faster method usually demands something in return. Here it demands order.

◈ ask an ai about this

“What is binary search, why does the list have to be sorted first, and how is it different from just checking every item?”

chatgpt ↗ claude ↗

why anyone cares

the gap is not small, it is absurd

On 32 boxes, one-at-a-time can take 32 looks and halving takes at most 6 — and only one target in the whole list actually needs that sixth look. Mildly interesting. Now watch what happens as the list grows, because this is the whole reason people study algorithms:

▤ worst case, side by side
list sizeone at a timehalving
3232 looks6 looks
1,0001,000 looks10 looks
1,000,0001,000,000 looks20 looks
8,000,000,0008 billion looks33 looks

Read the last row again. Every living human being, and you find the one you want in thirty-three questions. Each time the list doubles, halving needs exactly one more look. That is not a faster version of the same thing — it is a different kind of thing.

what this is called

Later you will meet a notation for this — O(n) for one-at-a-time, O(log n) for halving. It looks like maths and it is really just a way of writing down the shape of that table: what happens to the work when the problem gets bigger? That is the only question the notation answers.

◈ ask an ai about this

“What does Big O notation actually mean? Explain O(n) and O(log n) with an everyday example and no maths.”

chatgpt ↗ claude ↗

do this together

guess my number

☶ two people · 8 minutes · no equipment

Play it badly on purpose first. Then play it properly.

round one — the slow way

Person A thinks of a number from 1 to 100. Person B guesses 1, then 2, then 3… Count the guesses. Get bored. That boredom is the point.

round two — halving

Same game, but B always guesses the middle of what is left, and A answers only “higher” or “lower”. Count again. It will be seven or fewer, every time.

Then push it: 1 to 1,000 takes ten guesses. 1 to a million takes twenty. Try it and feel how little the game changes when the problem gets a thousand times bigger. Then ask the question that ruins it: what if A is allowed to lie once?

halfway

that is the thinking half

Six levels down. You can break a problem apart, write steps precise enough for something with no judgment, read the three shapes, trace values by hand, spot a problem you have already solved, and ask whether a correct answer is a good one.

The next six are the other half of what an intro course covers: what is actually inside the machine, why everything in it is numbers, what a variable really is, how conditions combine, how you know your work is right, and how order gets made.

what to keep

three things worth remembering

01

Right is the start, not the end

Once it works, ask what it costs. Both answers can be correct and only one usable.

02

Fast usually has a price

Halving needs a sorted list. Almost every speed-up demands something in return — find out what.

03

Ask how it grows

Not “how long does this take?” but “what happens when the input gets ten times bigger?”

check yourself

3 questions before you move on

Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.

▢ check yourself3 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

A sorted list of 1,000 items. Checking one at a time, how many might you have to look at in the worst case?

02

Same list of 1,000, but halving each time. How many looks now, at most?

03

What does halving require that checking-one-at-a-time does not?

answered: 0 of 3right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →