cadence_learn
think_like_a_programmer / level 12 of 12 ▶ 13 min

order makes everything cheap.

Sorting is what you do first so that everything after it gets easy. Watch one sort work honestly and slowly, then meet the idea that makes the real ones fast — the same idea from level two.

after this level you'll be able to
  • Describe a sorting method precisely enough for someone to follow it
  • Explain why doubling the list can quadruple the work
  • Say why sorting first can make everything afterwards cheaper
the whole idea

order is what makes everything else cheap

the one sentence

Sorting is rarely the point. It is what you do first, so that everything you want to do afterwards becomes easy — and it is the classic worked example of a problem with many correct answers of wildly different quality.

Remember level 6? Halving found a number in a list of eight billion in thirty-three looks, and it had one condition: the list had to be sorted. This level is that condition. Sort once, then search a thousand times cheaply — that trade is behind almost every fast thing a computer does.

one pass of a sort you would invent yourself
DONE — AND NEVER TOUCHED AGAIN STILL A MESS 2 5 9 11 14 31 40 27 18 55 33 SMALLEST LEFT swap it to the front of the mess Every pass: scan what is left, find the smallest, swap it into place. The green wall moves one step right, and never moves back. Ten items needs nine passes. A hundred items needs ninety-nine — and each pass has more to scan, which is where the cost comes from.
This is the sort you would invent yourself. Find the smallest, put it at the front, repeat with what is left — and the finished part never has to be looked at again.
try it yourself

watch it sort, one comparison at a time

Sixteen bars in a jumble. The method is the one you would invent yourself: look through what is left, find the smallest, swap it to the front. Then do it again with the rest.

▤ lab 12 · the sort

Green is finished. Amber is being looked at. Lime is the smallest found so far.

comparisons: 0 swaps: 0 sorted: 0 of 16
Press sort. Watch the green wall grow from the left and never move back.
◈ ask an ai about this

“What is selection sort, and why would anyone sort a list before searching it?”

chatgpt ↗ claude ↗

the cost

this one is honest, not fast

Count what it just did. Sixteen bars took around 120 comparisons — because the first pass looks at 15, the next at 14, and so on all the way down.

That adds up faster than it feels. Double the list and you roughly quadruple the work, because you are doing twice as many passes and each pass is twice as long. A hundred items is about 5,000 comparisons. A thousand is about half a million. A million items would be five hundred billion, which is a computer thinking for days.

so nobody uses this one for real work

The sorts inside real software — the one behind sorting a column in a spreadsheet — are cleverer, and they use the trick from level 2: split the problem in half, sort each half, then merge them. That gets a million items done in roughly twenty million steps instead of five hundred billion. Same problem, same correct answer, twenty-five thousand times less work.

You are not expected to write that one yet. What matters now is that you can see why one is worse: not because it is written badly, but because of the shape of how its work grows.

◈ ask an ai about this

“Why is merge sort so much faster than selection sort? Explain it without maths.”

chatgpt ↗ claude ↗

do this together

sort a deck by hand, twice

☶ two people · 12 minutes · a pack of cards

Deal sixteen cards face up. Now sort them — but only the way you are allowed to.

round one — the honest way

One of you may only compare two cards at a time and swap them. The other counts every single comparison out loud. Do not stop counting.

round two — split it

Deal into two piles of eight. Each person sorts their own pile. Then merge the two sorted piles by repeatedly taking the smaller card off the front. Count again.

Round two wins, and it wins by more as the pack grows. The reason is level 2 wearing different clothes — you broke the problem in half, solved the halves, and combined them. That is not a sorting trick; it is the single most reusable idea in the whole subject, and you have now met it twice.

where this leaves you

that is level zero, finished

Twelve levels, no C++, nothing installed. You can break a problem apart until the pieces are obvious and name them; write steps precise enough for something with no judgment; read the three shapes every program is made of; trace values by hand to find where it goes wrong; recognise a problem you have already solved; say why one correct answer beats another; explain what a processor and a compiler actually do; read a number in binary and know why decimals are approximate; describe what a variable is and why the equals sign lies; fill in a truth table; and choose test values on purpose.

That is the semester CSCI-100 would have given you, and it is the thinking CSCI-135 says its lectures focus on. Everyone else in that room has done this. Now so have you — and unlike most of them, you did it recently.

what comes next

Now the language. Setting up a Mac to compile C++ — and making sure what runs on your laptop also runs on the machines in the closed lab — is a genuinely fiddly afternoon that trips up more people in week one than any concept does. It is entirely solvable, and it is the next thing to build.

what to keep

three things worth remembering

01

Sort once, search forever

Order is an investment. You pay once so that everything after it gets cheap.

02

Doubling can quadruple

Watch the shape of the growth, not the speed on your laptop with ten items.

03

Split, solve, combine

The same idea as level 2, and the most reusable one in the subject.

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

Sorting 16 items by repeatedly finding the smallest takes how many comparisons? (15 + 14 + 13 …)

02

You double the list from 16 items to 32. Roughly what happens to the work?

03

Why sort a list at all, if you only want to find one thing in it?

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