Code

Radix sort — write it yourself

Counting sort works. The remaining functions have no holes this time: empty skeletons, and countingPass is essentially the counting sort you just wrote with a different key.

You already sorted years by hand in the table of step 2. Below is the same table written as pseudocode.

Radix sort

Counting sort needs an array of size k. For Year that is 131 — fine. But what if the key were a full int64?

Radix sort sidesteps that: it sorts one digit at a time, and for a single digit k is only 10.

RadixSortByYear(items):
    out ← copy of items
    for d from 0 to 3:                 // ones, tens, hundreds, thousands
        out ← countingPass(out, d)     // a stable counting sort
    return out

Least significant digit first. Four passes instead of one, but each with ten buckets instead of 131.

countingPass is the same three-pass counting sort, only the key is not Rating but a single digit of Year:

yearDigit(year, digit):
    divide year by 10, digit times, and return the remainder of dividing by 10

countingPass(items, digit):
    count ← 10 zeroes
    tally by yearDigit(it.Year, digit)
    prefix sums
    place BACKWARDS: out[count[v]-1] ← items[i], moves.Hit()

And this is where stability becomes load-bearing

When you sort by the tens digit, the ones digit is already in order. The second pass must preserve that — otherwise the first pass's work is gone. In the table that was 1964 and 1969.

Preserving it is exactly what a stable sort does.

In lesson 7 stability was a property. In lesson 8 it was a consequence. Here it is a correctness condition: without it, radix sort is not untidy — it is wrong.

In step 8 you will see that as two numbers and one false.

Your job

ncsort.go (in the panel) has grown: five new skeletons were added, each with panic("not implemented"):

  • yearDigit — one digit; 0 is the ones;
  • countingPass — a stable counting sort on one digit; k = 10;
  • countingPassUnstable — the same, but forwards. This is the "forwards" version you tried with a pencil in step 2;
  • RadixSortByYear — four countingPass calls, from the ones;
  • RadixSortByYearUnstable — the same four, through countingPassUnstable.

CountingSortByRating is still shown in the panel with its holes — the panel shows the skeleton, not your file. In your copy they are already filled in; leave them.

Read the contracts, not just the signatures.

ncsort_test.go has grown too and again requires the same numbers as the table: after each of the four passes the six years must stand as written in step 2, and every pass must make 6 moves.

Two things here are unusual:

A function that has to be wrong. RadixSortByYearUnstable must fail to sort, and must do the same amount of work as the stable version. It is broken deliberately, by one loop direction, and step 8 shows what for. If you "fix" countingPassUnstable, the test will say: the unstable version came out sorted — the demonstration has stopped working.

A wrong answer, written down in a test. The test also asserts the exact wrong result for the six years: 1969 1964 1987 1984 1999 2004 — the same one you saw in the warning in step 2.

First run

While the functions are empty, the test stops with a panic:

$ go test ./...
--- FAIL: TestYearDigit
panic: not implemented [recovered, repanicked]

The counting sort tests pass — they run first. A panic aborts the whole run, so work one function at a time: yearDigit (go test -run YearDigit), then countingPass (-run CountingPass), then both radix sorts.

When it does not pass

  • yearDigit(1987, 1) = 7, want 8 — you return only the last digit. Divide by 10 digit times before taking the remainder.
  • after pass 0 the years read [1984 1964 2004 ...] — countingPass walks forwards. That is the unstable variant; the stable one needs a backwards loop.
  • not sorted by Year and the stable version must sort correctly — the passes are in the wrong order. Start from the ones (digit 0), finish with the thousands.
  • the unstable version came out sorted — countingPassUnstable is the same as the stable one. It must go forwards.