Eight numbers, two algorithms
There is no code here. What there is, is what the two algorithms do — step by step, on the same eight numbers:
[6 3 7 1 8 2 5 4]
You write nothing in this step. Read the tables left to right and check at least one row with a pencil.
Two algorithms, one idea
Split in half, solve the parts, put them back together. Recursion is familiar from lesson 5 — there it was a way to walk a structure, here it is sorting.
They differ in where the work happens: in merge sort, on the way back up; in quicksort, on the way down.
How we count
Every row carries two numbers, written as comparisons / moves:
- a comparison — you looked at two elements and decided which is larger;
- a move — an element was written into its place or exchanged with another.
They are the same two counters you will see in the code as cmp and moves.
Merge sort
Splitting compares nothing: the slice is simply halved until single elements remain. All the work is merging two already-sorted halves, going from the bottom up:
start [6 3 7 1 8 2 5 4]
merge [6]+[3] → [3 6] 1 / 2
merge [7]+[1] → [1 7] 1 / 2
merge [3 6]+[1 7] → [1 3 6 7] 3 / 4
merge [8]+[2] → [2 8] 1 / 2
merge [5]+[4] → [4 5] 1 / 2
merge [2 8]+[4 5] → [2 4 5 8] 3 / 4
merge [1 3 6 7]+[2 4 5 8] → [1 2 3 4 5 6 7 8] 7 / 8
in total: 17 / 24
One row up close, [3 6]+[1 7] — this is where you see when the
comparisons stop:
left [3 6] right [1 7]
compare 3 with 1 → 1 is smaller, take it from the right
compare 3 with 7 → 3 is smaller, take it from the left
compare 6 with 7 → 6 is smaller, take it from the left
the left side is used up — 7 is moved WITHOUT a comparison
result: [1 3 6 7] 3 comparisons, 4 moves
The fourth element cost no comparison, but it cost a move. That is why every row has more moves than comparisons.
Quicksort
Here it is the other way round: nothing needs putting back together. All the work
is partitioning (partition) around one element, the pivot. Smaller items
stay on the left, the rest go to the right, and the pivot itself lands in its
final place:
start [6 3 7 1 8 2 5 4]
partition, pivot 6 [4 3 1 2 5] 6 [8 7] 7 / 6
partition [4 3 1 2 5], 4 [2 3 1] 4 [5] 4 / 4
partition [2 3 1], 2 [1] 2 [3] 2 / 2
partition [8 7], 8 [7] 8 [] 1 / 2
in total: 14 / 14
Parts of a single element ([5], [1], [3], [7]) are already sorted — they
are not partitioned again.
The first row up close. The pivot is the first element, 6. We sweep from
the left:
pivot 6 [6 3 7 1 8 2 5 4]
compare 3 → smaller — with itself (still counted) [6 3 7 1 8 2 5 4]
compare 7 → no — leave it [6 3 7 1 8 2 5 4]
compare 1 → smaller — swap with 7 [6 3 1 7 8 2 5 4]
compare 8 → no — leave it [6 3 1 7 8 2 5 4]
compare 2 → smaller — swap with 7 [6 3 1 2 8 7 5 4]
compare 5 → smaller — swap with 8 [6 3 1 2 5 7 8 4]
compare 4 → smaller — swap with 7 [6 3 1 2 5 4 8 7]
swap the pivot with the last "smaller" one [4 3 1 2 5 6 8 7]
Seven comparisons (one for every other element), six moves — five swaps and the final pivot swap.
The same numbers side by side
| comparisons | moves | if the slice is already [1 2 3 4 5 6 7 8] |
|
|---|---|---|---|
| merge | 17 | 24 | 12 cmp / 24 moves |
| quick (first-element pivot) | 14 | 14 | ? |
Three observations.
1. Merge sort's moves are exactly 24 = 8 · 3. Three merge levels (2³ = 8), and every level writes all eight elements. That number does not depend on the order — a sorted slice costs exactly as many. Only the comparisons change: 17 → 12, because when the left side runs out at once, the right side is appended without comparing.
2. Quicksort works in place, so it moves less — 14, not 24. It only moves what lies on the wrong side.
3. Do not draw conclusions from eight numbers. Here quicksort also made fewer comparisons (14 against 17). With 10,000 shuffled items the picture flips — you will see that in step 8. Eight numbers show where the work happens, not who wins.
Guess before reading on
One cell in the table is empty. Fill it in with a pencil before you read on.
Take [1 2 3 4 5 6 7 8] and partition it once with a first-element pivot.
Where does the pivot land? How big is the right-hand part? And how many such
partitions will it take?
In lesson 7, -order sorted was insertion sort's best case. Most people
assume sorted data is easier work for quicksort too.
What comes next
You now know what both algorithms do. In the next step you write one of them as Go code — merge sort, because the whole idea fits in it, along with the one character where stability lives.