Trace a sort on repeated keys
Quick review (2 minutes)
Sorting a hand of cards by picking out the smallest, then the smallest of the rest, and so on, shows the idea of this lesson.
Selection sort moves the smallest unsorted card to the sorted row, pass after pass, until no unsorted card is left, and finding the smallest of n unsorted cards takes n - 1 comparisons. With this lesson's rules, a card replaces the smallest so far only if its key is smaller, and the cards left unsorted keep their order; then the leftmost of equal smallest cards moves first, and equal keys keep their order.
Write the unsorted row with each card told apart, and an empty sorted row.
Mark the first unsorted card as the smallest so far.
Compare the marked card with each later unsorted card in turn, counting each comparison. When a card's key is smaller than the marked card's key, move the mark to that card, and compare the cards after it with the newly marked card.
Move the marked card to the end of the sorted row, and close up the unsorted row without changing its order.
Repeat until no unsorted card is left, then add up the comparisons.
One word changes the order of equal keys. If the mark switched to a card that is smaller or equal, pass 3 would move the blue 4 first, and the sorted row would end blue 4, red 4. Both rows are sorted by key, but only one keeps the start order of the fours. Write the test you mean.
A second slip is comparing against cards already sorted. Each pass compares only unsorted cards, which is why the count falls by one each pass.
1 In plain words
Sorting a hand of cards by picking out the smallest, then the smallest of the rest, and so on, shows the idea of this lesson.
Selection sort works that way. It finds the smallest card in the unsorted row and moves it to the end of a sorted row, then repeats with the cards that are left until none is left.
The number on a card is its key, the value the sort puts in order. Two cards can share a key. In this lesson's invented row, a red 4 and a blue 4 both have the key 4, and the color shows which is which.
The selection sort of this lesson finds the smallest card in a fixed way. Take the first unsorted card as the smallest so far. Compare it with each later unsorted card in turn, and a card replaces it only when that card's key is smaller. Then move the smallest so far to the sorted row; the other unsorted cards keep their order. These are this lesson's own rules for carrying out the selection sort the Field Guide describes.
Remember one sentence: select the smallest, move it, repeat. To look up selection sort, see section 2.3.2 of the Computer Science Field Guide's algorithms chapter.
2 Picture it
A table of the four passes of selection sort on the row red 4, 2, blue 4, 1. Pass 1 moves the 1 after 3 comparisons. Pass 2 moves the 2 after 2 comparisons. Pass 3 moves the red 4 after 1 comparison. Pass 4 moves the blue 4 with no comparison. The row for pass 3 is marked.
3 Why
Finding the smallest of a group takes one comparison for each card after the first. The Field Guide gives the pattern: finding the lightest of 8 boxes takes 7 comparisons, and finding the next lightest of the remaining boxes takes 6. On 4 cards the passes take 3, 2, 1 and 0 comparisons, 6 in all.
The replacement test decides what happens to equal keys. On pass 3 the unsorted cards are the red 4 and then the blue 4. The blue 4 is not smaller than the red 4, so the red 4 stays the smallest so far and moves first. With this test the leftmost of several equal smallest cards is always the one moved, so equal keys keep their order from the start row.
4 The rule
Selection sort moves the smallest unsorted card to the sorted row, pass after pass, until no unsorted card is left, and finding the smallest of n unsorted cards takes n - 1 comparisons.
With this lesson's rules, a card replaces the smallest so far only if its key is smaller, and the cards left unsorted keep their order; then the leftmost of equal smallest cards moves first, and equal keys keep their order.
5 How
- Write the unsorted row with each card told apart, and an empty sorted row.
- Mark the first unsorted card as the smallest so far.
- Compare the marked card with each later unsorted card in turn, counting each comparison. When a card's key is smaller than the marked card's key, move the mark to that card, and compare the cards after it with the newly marked card.
- Move the marked card to the end of the sorted row, and close up the unsorted row without changing its order.
- Repeat until no unsorted card is left, then add up the comparisons.
6 Trace
| Pass | Unsorted row | Sorted row | Explanation |
|---|---|---|---|
| start | red 4, 2, blue 4, 1 | — | Nothing is sorted yet. |
| 1 | red 4, 2, blue 4 | 1 | The mark moves from red 4 to 2, then to 1: 3 comparisons. The 1 moves. |
| 2 | red 4, blue 4 | 1, 2 | The mark moves from red 4 to 2. Blue 4 is then compared with 2 and is not smaller: 2 comparisons. The 2 moves. |
| 3 | blue 4 | 1, 2, red 4 | Blue 4 is not smaller than red 4, so the mark stays on red 4: 1 comparison. Red 4 moves. |
| 4 | — | 1, 2, red 4, blue 4 | One card is left and needs no comparison. Blue 4 moves. 6 comparisons in all. |
7 The trap
One word changes the order of equal keys. If the mark switched to a card that is smaller or equal, pass 3 would move the blue 4 first, and the sorted row would end blue 4, red 4. Both rows are sorted by key, but only one keeps the start order of the fours. Write the test you mean.
A second slip is comparing against cards already sorted. Each pass compares only unsorted cards, which is why the count falls by one each pass.
Quick checks
Quick check 1 of 2
Selection sort with this lesson's steps puts a row of 5 cards in order. How many comparisons does it make in all?
Show the steps and the answer
It makes 10 comparisons.
Why: Finding the smallest of 5, 4, 3, 2 and 1 cards takes 4, 3, 2, 1 and 0 comparisons, and 4 + 3 + 2 + 1 is 10.
Quick check 2 of 2
Suppose the mark switched to a card whose key is smaller or equal. Which four moves first when this changed selection sort sorts the row red 4, 2, blue 4, 1?
Show the steps and the answer
The blue 4 moves first, and the sorted row is 1, 2, blue 4, red 4.
Why: With this lesson's rules and that one change, the first two passes move the 1 and the 2, so on pass 3 the unsorted row is red 4, blue 4. The blue 4 is equal to the red 4, so with the changed test the mark switches to it, and it moves first.
Sources
- Computer Science Field Guide: Algorithms, 2.3 Sorting. University of Canterbury Computer Science Education Research Group. CC BY-SA 4.0. Read 2 Dec 2026.