Selection and insertion sort
Trace selection, insertion and bubble sort on one list, count each one's comparisons in the best and worst case, and say which are stable.
It opens on
Selection sort finds the smallest unsorted item and swaps it to the front, again and again. On 6 items, does a list already in order or one in reverse need more comparisons?
Why it matters
Whether a sort does n² steps on every input, or only on bad ones, decides which one a program uses for a list that is already nearly in order, and whether sorting by two keys in turn keeps the first order.
Good to know first
- Analyzing loops: a loop whose inner count is 1, 2, ..., n − 1 takes n(n − 1)/2 steps
- Big-O, Big-Omega and Big-Theta: a Θ bound says the cost is of that order, and best, worst and average cases can differ
- Selection sort and insertion sort: both were written and traced pass by pass
- Lists: indexing and mutation: a[i], a[m] = a[m], a[i] swaps two items
Step by step
Selection sort
Trace selection sort on a list, and show that it makes n(n − 1)/2 comparisons on every input and at most n − 1 swaps.
In the first pass i is 0 and m is 0. The scan compares 3 with 5, so m becomes 1, then 8 with 3, then 1 with 3, so m becomes 3, then 9 with 1 and 2 with 1. That is 5 comparisons, and the swap of a[0] and a[3] gives 1, 3, 8, 5, 9, 2.
A common mix-up
Selection sort is faster on a list that is already sorted.
In fact: The scan still compares every item with the smallest so far, so a sorted list takes the same n(n − 1)/2 comparisons.
Insertion sort
Trace insertion sort, and show that it makes n − 1 comparisons on sorted input and n(n − 1)/2 on reversed input, with one shift for each pair out of order.
With the key 1, the loop compares it with 8, then 5, then 3, and each is larger, so each shifts right. Then j reaches −1 and the loop ends without a comparison. That is 3 comparisons and 3 shifts, and the key goes to place 0, giving 1, 3, 5, 8, 9, 2.
A common mix-up
Insertion sort is Θ(n²), so it is slow on every input.
In fact: Θ(n²) is the worst case; on a sorted list it makes n − 1 comparisons, and the cost follows the number of inversions.
Bubble sort and stability
Trace bubble sort with an early exit, and say which of selection, insertion and bubble sort keeps equal items in their original order.
On our list 5, 3, 8, 1, 9, 2 the first pass swaps 5 and 3, makes no swap for 5 and 8, swaps 8 and 1, makes no swap for 8 and 9, and swaps 9 and 2. That is 5 comparisons and 3 swaps, the list is 3, 5, 1, 8, 2, 9, and the 9 is in place.
A common mix-up
A stable sort never swaps.
In fact: Stable means equal items keep their order, and bubble sort swaps often but never swaps equal neighbours, because its test is strict.
Try it
Selection sort on 4, 7, 1, 9, 3. What is the list after the first two passes, and how many comparisons in all?Answer
1, 3, 4, 9, 7; 10 comparisons
Hint: Pass 0 swaps the 4 and the 1, and the total is 4 + 3 + 2 + 1
Insertion sort on 6, 2, 5, 3, 1. How many shifts in all?Answer
8
Hint: Count the pairs out of order, the inversions
Bubble sort with early exit on 2, 1, 3, 4. How many passes and how many comparisons?Answer
2 passes; 3 + 2 = 5 comparisons
Hint: The second pass makes no swap, and the passes make 3 and then 2 comparisons
Pass 1 has i equal to 1, so the unsorted part is 3, 8, 5, 9, 2. Which item does it select, and what is the list after the swap?Answer
The 2, which is at place 5, and the list is 1, 2, 8, 5, 9, 3.
The next key is the 9. How many comparisons and how many shifts does it need?Answer
1 comparison, since 8 is not larger than 9, and no shifts.
