Code

Bubble and selection sort — write them yourself

Insertion sort works. The other two — bubble and selection — come without holes this time: empty skeletons, and you fill them in completely.

You have already seen both in the last step's tables. Below are the same tables written as pseudocode.

Bubble sort

end ← n - 1
repeat:
    swapped ← false
    for i from 0 to end-1:
        cmp.Hit()
        if items[i].Title > items[i+1].Title:
            swap items[i] and items[i+1]
            swap.Hit()
            swapped ← true
    end ← end - 1
    if not swapped: stop

The swapped flag is not decoration: without it, bubble sort makes all n passes even over an already-sorted slice. With it, one.

end ← end - 1 is why every pass is shorter than the last (4, 3, 2, 1 comparisons in the table). Without it the test will not accept your numbers.

Strictly >, never >=. Swapping equal items would destroy stability.

Selection sort

for i from 0 to n-2:
    min ← i
    for j from i+1 to n-1:
        cmp.Hit()
        if items[j].Title < items[min].Title:
            min ← j
    if min ≠ i:
        swap items[i] and items[min]
        swap.Hit()

Notice that the inner loop always runs to the end. Sorted or not, there is no other way to know which remaining item is smallest. So selection sort has no best case, and the test demands exactly that.

In exchange you get one thing: at most n−1 swaps. Of the three, it moves the least data.

if min ≠ i — swap only when the smallest item is not yet home. In the table, the i=1 row swapped nothing.

Stability

A sort is stable if equal keys keep their original relative order.

Bubble and insertion are stable because they only ever exchange neighbours: two equal items can never cross.

Selection sort is not. It throws an item across an arbitrary distance, and that jump can carry one equal item past another. The test asserts this: if your selection sort ever comes out stable, the test will say so — and then it is worth checking what that cost in swaps.

Your job

sort.go (in the panel) has grown: two new skeletons, BubbleSort and SelectionSort, each with panic("not implemented").

InsertionSort still shows up in the panel with its holes — the panel shows the skeleton, not your file. In your copy they are already filled in; leave them that way.

Read the contracts, not just the signatures. The doc comments state everything the test will check:

  • BubbleSort — stable; every pass shorter than the one before; exactly n−1 comparisons on sorted input;
  • SelectionSort — not stable; exactly n(n−1)/2 comparisons on any input; at most n−1 swaps.

sort_test.go has grown too: it now checks all three, and demands the same numbers as the table for each:

[5 2 4 1 3] comparisons swaps
bubble 10 7
selection 10 3

And one test asserts a defect: that selection sort is not stable. It is written that way so the defect stays visible.

Run go test ./.... While you write, the test tells you which algorithm disagrees and where — the same messages as in the last step.