Merge sort — fill in two holes
In the last step you watched merge sort in a table. Now the same thing is written down as an algorithm — and you fill in two missing pieces.
Pseudocode
MergeSort(items):
if len(items) <= 1: return a copy
mid ← len(items) / 2
left ← MergeSort(items[:mid])
right ← MergeSort(items[mid:])
return Merge(left, right)
Merge(a, b):
out ← empty, capacity len(a)+len(b)
i ← 0; j ← 0
while i < len(a) and j < len(b):
cmp.Hit()
if a[i].Title <= b[j].Title:
out ← out + a[i]; i ← i + 1
else:
out ← out + b[j]; j ← j + 1
moves.Hit()
append what is left of a, then of b (each with moves.Hit())
return out
Splitting compares nothing. All the work is Merge, and it is linear: every
element is touched once. There are log₂ n levels with n work at each — that is
where O(n log n) comes from.
The recursion is already written. What is left is merge, which is exactly the
row you just did by hand: [3 6]+[1 7].
One character where stability lives
if a[i].Title <= b[j].Title
That <= means: on a tie, the LEFT side wins. The left side is the one that
came earlier in the slice — so equal elements keep their original order.
Write < instead of <= and the sort still works. Tests that only check the
order will pass. But the stability is gone — and where that matters you will see
in step 6.
Your job
Create dcsort.go and dcsort_test.go (see the panel).
dcsort.go is complete and compiles, but it does not work: MergeSort and
the loops are written and two places are missing. They are marked HOLE 1 and
HOLE 2:
- HOLE 1 — the condition for taking from the left side. In the pseudocode
that is
if a[i].Title <= b[j].Title. Replacefalsewith a real condition. - HOLE 2 — the leftovers. The loop ends when one side is used up; what is
left of the other side has to be appended and counted (
moves.Hit()). In the last step that was the7, which was moved without a comparison.
Nothing else needs changing. The loop already contains cmp.Hit(), i++, j++
and moves.Hit().
dcsort_test.go is the verdict — you do not write it, you pass it. For now it
checks merge sort only.
The test also checks the table from the last step: [6 3 7 1 8 2 5 4] must take
17 comparisons and 24 moves, and [1 2 3 4 5 6 7 8] 12 and 24. If your
code counts differently, something is off even if the result comes out sorted.
One thing that is easy to miss
MergeSort returns a new slice rather than sorting in place, and the test
checks that the input is left alone.
That is not fussiness — it is merge sort's price. It needs somewhere to merge into, and that somewhere is n extra elements. Quicksort needs none, and in step 8 you will see that this is why it is often faster despite making more comparisons.
Run the test before you fill anything in
Run it as it stands first:
$ go test ./...
--- FAIL: TestMergeSortSorts
--- FAIL: TestMergeSortSorts/n=3
dcsort_test.go:44: returned 1 items, want 3
--- FAIL: TestMergeSortIsStable
dcsort_test.go:69: position 0 has ID 5, want 2 — not stable (got [5])
--- FAIL: TestMergeSortReproducesTheTable
--- FAIL: TestMergeSortReproducesTheTable/shuffled_6_3_7_1_8_2_5_4
dcsort_test.go:105: comparisons / moves = 7 / 7, want 17 / 24
--- FAIL: TestMergeSortReproducesTheTable/already_sorted_1_2_3_4_5_6_7_8
dcsort_test.go:112: comparisons / moves = 7 / 7, want 12 / 24
FAIL
That is not an error — it is the starting point. The skeleton always takes from
the right and never appends the leftovers, so every merge returns a single
element: returned 1 items, want 3. Hence 7 comparisons and 7 moves instead of
17 and 24 — one each for the seven merges.
Fill in both holes and run it again — you should see ok.
When it does not pass
not stable (got [4 2 5 3 1])— you most likely wrote<instead of<=. Equal items jump over each other: the right side wins ties.comparisons / moves = 17 / 17, want 17 / 24— you appended the leftovers but did not count them. Every appended element is onemoves.Hit().panic: index out of rangeorreturned 1 items— you forgot HOLE 2. One ofa,balways runs out first, and the other's remainder is lost.not sorted— the condition is the wrong way round (>=instead of<=): you take from the left when it is larger.