Five numbers, three sorts
There is no code here. What is here is what the algorithms do — one step at a time, on the same five numbers:
[5 2 4 1 3]
You write nothing in this step. Read the tables left to right and check at least one row with a pencil.
How we count
Every row carries two numbers, written as comparisons / swaps:
- comparison — you looked at two items and decided which is larger;
- swap — an item changed place. In insertion sort that is a shift by one slot.
These are the same two counters you will see in the code as cmp and swap.
Insertion sort
Take one item at a time, left to right. Everything to its left is already in order. Push the item left until it sits in its place.
start [5 2 4 1 3]
i=1 take 2 [2 5 4 1 3] 1 / 1
i=2 take 4 [2 4 5 1 3] 2 / 1
i=3 take 1 [1 2 4 5 3] 3 / 3
i=4 take 3 [1 2 3 4 5] 3 / 2
total: 9 / 7
One row up close, i=4 — this is where you see when the pushing stops:
take 3: [1 2 4 5 | 3]
compare 3 with 5 → 5 is larger, shift it right
compare 3 with 4 → 4 is larger, shift it right
compare 3 with 2 → 2 is NOT larger — STOP
put 3 in the free slot: [1 2 3 4 5]
The 1 was never even touched. The algorithm stopped the moment it found the
place.
Bubble sort
Walk the slice and swap every neighbouring pair that is out of order. After one pass the largest item is at the end and is never touched again.
start [5 2 4 1 3]
pass 1 [2 4 1 3 5] 4 / 4 5 travelled to the end
pass 2 [2 1 3 4 5] 3 / 2
pass 3 [1 2 3 4 5] 2 / 1
pass 4 [1 2 3 4 5] 1 / 0 no swaps at all — done
total: 10 / 7
Every pass is shorter than the last: 4, 3, 2, 1 comparisons, because the tail is already final.
Selection sort
Find the smallest remaining item and swap it into place. Once per position.
start [5 2 4 1 3]
i=0 smallest 1 [1 2 4 5 3] 4 / 1 5 and 1 swapped
i=1 smallest 2 [1 2 4 5 3] 3 / 0 2 is already home
i=2 smallest 3 [1 2 3 5 4] 2 / 1
i=3 smallest 4 [1 2 3 4 5] 1 / 1
total: 10 / 3
The same numbers side by side
| comparisons | swaps | if the slice is already [1 2 3 4 5] |
|
|---|---|---|---|
| insertion | 9 | 7 | 4 cmp / 0 swaps |
| bubble | 10 | 7 | 4 cmp / 0 swaps |
| selection | 10 | 3 | 10 cmp / 0 swaps |
Three observations. Do not skip them — the later steps are built from nothing else.
1. Insertion and bubble both make 7 swaps. That is not a coincidence.
[5 2 4 1 3] contains exactly seven pairs that are out of order:
(5,2) (5,4) (5,1) (5,3) (2,1) (4,1) (4,3). Swapping two neighbours fixes
exactly one pair. However cleverly you do it, if you only ever exchange
neighbours you cannot avoid those seven swaps.
From n items you can form up to n(n−1)/2 pairs. That is where n² comes from.
2. Selection sort swaps only 3 times, but makes 10 comparisons — and would make 10 for any five-element slice.
3. On a sorted slice insertion and bubble need only n−1 = 4 comparisons. Selection still makes 10 — it has no way to learn that there is nothing to do. That is exactly what "has no best case" means.
What next
Now you know what each algorithm does. In the next step you write one of them as Go code — the easiest one, insertion sort.