Code

The harness and the verdict

Three files in one step. Two of them you are given complete — they are not the 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".

sort_test.go — the verdict. You do not write it; you pass it.

And main.go — two lines, as usual.

What the test demands

Four groups:

  1. It sorts — including n = 0, 1, 2 and 3. An empty slice and a one-element slice must pass without any special-case branches in your code.
  2. Stability — bubble and insertion. Checked through the IDs, because only they reveal which items moved past which.
  3. Instability — selection. This test asserts a defect, so that it stays visible.
  4. Exact counts — n−1, n(n−1)/2, and the quadratic ceiling.

If the test passes, your three functions are correct and have the correct cost.