Code

The harness and the verdict

Three files, as in lessons 7 and 8.

ncbench.go adds algo count and algo radix. The first compares counting sort against comparison sorts on the same key; the second runs radix sort with both inner passes and shows the difference.

Note what algo radix prints: not just sorted?, but the first place the unstable version breaks. Showing where a result went wrong is more useful than reporting that it did.

ncsort_test.go — the verdict, with three assertions no earlier lesson made: zero comparisons, exactly n moves, and that the unstable version fails to sort.

main.go — two lines.

Why the comparison uses the same key

algo count sorts by Rating with both counting sort and sort.SliceStable. The same key on the same data — otherwise the comparison would mean nothing.

The insertion-sort row in that table sorts by Title, and is labelled as such. It is there for scale, not as a like-for-like comparison.