Selection sort and insertion sort
Trace and write selection sort and insertion sort pass by pass, and count their comparisons as about n²/2 in the worst case.
It opens on
Selection sort finds the smallest item and swaps it to the front, then repeats on the rest. Sorting [29, 10, 14, 37, 13], what does the list look like after that first swap?
Why it matters
Sorting is everywhere, and these two simple sorts are where students first trace an algorithm pass by pass and count its work; showing the list after each pass and counting comparisons are standard exam questions, and the quadratic count explains why faster sorts exist.
Good to know first
- Nested loops: an inner loop runs fully for each pass of the outer one
- Tuples and unpacking: xs[i], xs[j] = xs[j], xs[i] swaps two items
- Linear and binary search: counting comparisons and the idea of the worst case
Step by step
Selection sort
Trace selection sort pass by pass, swapping the smallest remaining item into place, and write it with nested loops.
Pass 1 scans 29, 14, 37, 13 for the smallest, 13, and swaps it with the 29 at index 1, giving 10, 13, 14, 37, 29.
A common mix-up
Selection sort swaps every time it finds a smaller item.
In fact: It only remembers the index while scanning, and does one swap at the end of each pass.
Insertion sort
Trace insertion sort pass by pass, sliding each item left into the sorted front, and write it with a while loop.
Pass 1 takes 10. It is smaller than 29, so 29 shifts right and 10 goes in front, giving 10, 29, 14, 37, 13.
insertion sort passes
A common mix-up
Insertion sort swaps the new item with each bigger one.
In fact: It shifts the bigger items right one place each and writes the new item once, into the gap.
Counting comparisons
Count selection sort's comparisons as n(n − 1)/2 and insertion sort's as between n − 1 and n(n − 1)/2, both O(n²) in the worst case.
That adds up to n(n − 1)/2, which is 45 for 10 items and 4950 for 100, the counts the run printed. It is the same for every list, sorted or not, since the scan never stops early.
A common mix-up
Insertion sort and selection sort always make the same number of comparisons.
In fact: Selection sort always makes n(n − 1)/2; insertion sort made 999 on a sorted list of 1000 and 248100 on a random one.
Try it
Give the list after each pass of selection sort on [5, 1, 4, 2].Answer
1, 5, 4, 2; then 1, 2, 4, 5; then 1, 2, 4, 5 unchanged
Hint: The second pass finds 2
Give the list after each pass of insertion sort on [5, 1, 4, 2].Answer
1, 5, 4, 2; then 1, 4, 5, 2; then 1, 2, 4, 5
Hint: Pass 3 slides 2 past 5 and 4
How many comparisons does selection sort make on 20 items?Answer
190, which is 20 × 19 / 2
Hint: 19 + 18 + ... + 1
In [29, 10, 14, 37, 13], the smallest is 10. What is the list after swapping it with the 29 at the front?Answer
10, 29, 14, 37, 13, which answers the opening question.
Pass 2 takes 14. Where does it go, and how many items does it compare with?Answer
Between 10 and 29; it compares with 29, shifts it, then with 10 and stops, so 2 comparisons.
