Code

The measuring harness

The test passes — your three functions are correct. Now we measure them.

Two files in this step, both complete. They are not your exercise.

sortbench.go — the measuring harness. prepare puts the data in the requested order (shuffled, sorted, reverse, nearly), and cmdSort runs the chosen algorithms and prints the numbers.

One thing in it matters:

if !isSortedByTitle(data) {
    return fmt.Errorf("%s did NOT sort the data", name)
}

Every measurement checks the result first. The speed of an incorrect sort means nothing — and an incorrect sort often looks fast, precisely because it did less work. In lesson 6 the same principle was phrased as "prove they are the same function first".

It is the same reason the test and the measurement are two separate things: the test says whether a function is correct; the measurement says whether it is cheap.

And main.go — two lines, as usual: the sort command in usage and in the switch.

First run

A thousand items cost nothing yet — you will see the real curve in the next step. But make sure the command works:

$ algo gen -n 1000 -seed 42 -o s1k.jsonl
$ algo sort -in s1k.jsonl

A three-row table should appear: comparisons, swaps, time. The same two columns you have been counting by hand until now.