Theory

Seven items, six years

There is no code here. What there is, is what two algorithms do — step by step, on very small numbers so you can see all of it.

You write nothing in this step. Read the tables left to right and check at least one row with a pencil.

How we count

This time the cmp counter stays at zero, so the rows carry only moves — how many elements were written into their place. There are no comparisons. That is the whole point of the lesson, and you can see it without any code.

Counting sort

Seven items. The letter says which item; the digit is its Rating:

A3 B1 C3 D0 E2 F1 G3

The letters exist only so you can see which of the equal items ended up where.

start                   A3 B1 C3 D0 E2 F1 G3

1. tally                value        0   1   2   3
                        how many     1   2   1   3

2. prefix sums          value        0   1   2   3
                        running      1   3   4   7

3. place BACKWARDS
   G3  count[3]: 7 → 6   slot 6      [. . . . . . G]
   F1  count[1]: 3 → 2   slot 2      [. . F . . . G]
   E2  count[2]: 4 → 3   slot 3      [. . F E . . G]
   D0  count[0]: 1 → 0   slot 0      [D . F E . . G]
   C3  count[3]: 6 → 5   slot 5      [D . F E . C G]
   B1  count[1]: 2 → 1   slot 1      [D B F E . C G]
   A3  count[3]: 5 → 4   slot 4      [D B F E A C G]

result                  D0 B1 F1 E2 A3 C3 G3         0 comparisons / 7 moves

In the real code count has 101 slots, not 4: Rating runs from 0 to 100. The table shows only 0–3 because the other 97 stay zero (and after the prefix sums they would all hold 7).

One row up close, C3:

count[3] is now 6  →  the 3s share slots 4, 5, 6;  slot 6 is taken (G)
subtract one       →  count[3] = 5
put C in slot 5    →  [D . F E . C G]

Three observations.

1. At no step were two items compared. The first pass reads the keys, the second adds up whole numbers, the third uses the key as an address. 0 comparisons, 7 moves — one per item.

2. A prefix sum is "how many are not larger". count[2] = 4 means four items have a rating ≤ 2, so the slots for 3s start at 4. The block of 3s is slots 4, 5, 6 — three slots, as many as there are 3s.

3. The three 3s came out A, C, G — in the order they went in. The same for the 1s: B, F. That is because we walked backwards: the last item landed in the last slot of its block.

Try the same table with a pencil walking forwards, from A. You get D F B E G C A — the 1s (F, B) and the 3s (G, C, A) reversed. The same numbers, the same moves, only the loop direction. You will write that variant on purpose in step 4.

Radix sort

Six years:

1987 2004 1969 1964 1999 1984

Four passes, each the same counting sort but on one digit (k = 10), starting from the ones:

start                      1987 2004 1969 1964 1999 1984

pass 1 (ones)              2004 1964 1984 1987 1969 1999     digits 4 4 4 7 9 9
pass 2 (tens)              2004 1964 1969 1984 1987 1999     digits 0 6 6 8 8 9
pass 3 (hundreds)          2004 1964 1969 1984 1987 1999     digits 0 9 9 9 9 9
pass 4 (thousands)         1964 1969 1984 1987 1999 2004     digits 1 1 1 1 1 2

                                          in total:  0 comparisons / 24 moves

Two observations and one warning.

1. After the first pass the slice is not sorted yet — 2004 stands first. It is sorted only by the last digit. It becomes fully sorted only after the final pass.

2. The second pass has to preserve the first pass's work. Look at 1964 and 1969: both have tens digit 6, so the second pass does not compare them. Their order (4 before 9) was decided by the first pass, and the second must not undo it. In the third pass all digits but one are equal and nothing moves — a pass in which every key is equal must be no work at all.

Warning. With an unstable inner pass the same six years would end like this: 1969 1964 1987 1984 1999 2004. The same 24 moves, 0 comparisons — and a wrong answer. You will see that in step 8, with a hundred thousand items.

What comes next

You now know what both algorithms do. In the next step you write counting sort as Go code, and run exactly these seven items through it.