Code

Quicksort — write it yourself

Merge sort works. This time there are no holes for quicksort: empty skeletons, and you write the whole partitioning yourself.

You already did it by hand in the last step — the first row of [6 3 7 1 8 2 5 4] with pivot 6. Below is the same table written as pseudocode.

Pseudocode

QuickSort(items):
    if len(items) <= 1: return
    p ← Partition(items, pivotIndex)
    QuickSort(items[:p])
    QuickSort(items[p+1:])

Partition(items, pivotIdx):
    move items[pivotIdx] to the front (moves.Hit(), if it moved)
    pivot ← items[0].Title
    i ← 1
    for j from 1 to len(items)-1:
        cmp.Hit()
        if items[j].Title < pivot:
            swap items[i] and items[j];  moves.Hit();  i ← i + 1
    swap items[0] and items[i-1];  moves.Hit()
    return i-1

This is the other way round from merge sort: all the work is in the partitioning, and there is nothing to put back together — once both sides are sorted, the slice is sorted.

And it works in place: no second slice.

Count every swap, even when i == j and an element is swapped with itself — in the table the first comparison (3) is exactly that. The test checks the first row: 7 comparisons, 6 moves.

But the pivot is a guess

Partition splits the slice at the pivot. If the pivot is the median you get two equal halves and a depth of log₂ n.

If the pivot is the smallest element, the left side is empty and the right side holds n−1 elements. And so on, at every step.

When is the pivot always the smallest? When you take the first element and the slice is already sorted. You wrote that guess down in the last step.

Three of the four functions are the same quicksort with a different pivot, so you write partition once and use it three times:

  • QuickSortNaive — always the first element (pivotIdx = 0);
  • QuickSortMedian3 — the median of the first, middle and last. Choosing the pivot costs three comparisons per partition, counted with cmp.Hit();
  • QuickSortRandom — a random index. The seed is fixed (rand.New(rand.NewSource(1))), created once and passed down the whole recursion so the counts repeat. You will need the math/rand import.

Your job

dcsort.go (in the panel) has grown: partition and three quicksort skeletons were added, each with panic("not implemented").

merge 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. The doc comments spell out everything the test will check:

  • partition — returns the pivot's final index; strictly smaller items on the left; every exchange counted;
  • QuickSortNaive — exactly n(n−1)/2 comparisons on sorted input. The test demands that precise number, because the failure is the lesson;
  • QuickSortMedian3 — far below quadratic on sorted input;
  • QuickSortRandom — only the correct order.

dcsort_test.go has grown too: besides merge sort it now checks the quicksorts, and again requires the same numbers as the table:

[6 3 7 1 8 2 5 4] comparisons moves
the first partition 7 6
QuickSortNaive in total 14 14

Two tests assert a defect:

TestQuickSortIsNotStable — that quicksort is not stable. It is the same shape as lesson 7's selection-sort test: if your quicksort ever comes out stable, the test will say so — and then it is worth checking what that cost.

TestNaivePivotIsQuadraticOnSortedInput — the worst case. Not "slow", but exactly n(n−1)/2. That line exists so the failure stays written down instead of being quietly fixed and forgotten.

First run

While partition is empty, the test stops at the first quicksort with a panic:

$ go test ./...
--- FAIL: TestQuickSortsSort/naive_n=0
panic: not implemented [recovered, repanicked]

The merge sort tests pass — they run first. A panic aborts the whole run, so work one function at a time: partition first, checked alone with go test -run Partition, then QuickSortNaive.

When it does not pass

  • comparisons / moves = 7 / 5, want 7 / 6 — you did not count the last swap (the pivot into place). It is a move too.
  • not sorted, for n ≥ 3 — the pivot stays at the front. After the loop it has to be swapped with the last "smaller" item (items[i-1]).
  • fatal error: stack overflow — the recursion does not shrink. The right part is items[p+1:], not items[p:]: the pivot is already in place.
  • QuickSortNaive ... want exactly 130816 — you got fewer. You most likely chose a pivot other than the first element. This algorithm must be bad.