Insert one card into sorted order
Quick review (2 minutes)
A library buys one new book.
With this lesson's steps, a new card goes into a sorted row like this: compare it with the cards from the right-hand end and place it directly to the right of the first card that is not larger, or first in the row when every card is larger. The row stays sorted. Insertion sort, as the Field Guide describes it, inserts each card in turn into the sorted row so far.
Write the sorted row and the new card.
Compare the new card with the last card, and count 1.
If the card compared is not larger, place the new card directly to its right: the insertion is done.
If no card is left to its left, place the new card first: the insertion is done. Otherwise compare with the next card to the left, count 1 and go back to the third step.
When the insertion is done, check that the new row is sorted and holds every card once.
The case with no stopping card needs its own step. Inserting 2 into 4 9 13 passes 13, 9 and 4, all larger, and reaches the left-hand end with no card that is not larger. The step for that case places the new card first, giving 2 4 9 13.
A second slip is placing the new card to the left of the stopping card. Inserting 12 next to 11 that way gives 3 8 12 11 15 20, which is not sorted. The new card goes to the right of the first card that is not larger.
1 In plain words
A library buys one new book. Nobody re-sorts the whole shelf: the new book goes into its place, and every other book stays in its order.
Insertion puts one new card into a sorted row of one or more cards at its correct position. The insertion of this lesson compares from the right-hand end. Step 1: compare the new card with the last card of the row. Step 2: if the card compared is not larger than the new card, place the new card directly to its right and stop. Step 3: if no card is left to the left of the card compared, place the new card first and stop. Step 4: compare the new card with the next card to the left and go to step 2. These steps are this lesson's own way of finding the correct position the Field Guide describes.
Insertion sort builds a whole sorted row this way: it starts with one card and inserts each remaining card, one at a time, into the sorted row so far.
Remember one sentence: compare from the end and stop at the first card that is not larger. To look up insertion sort, see section 2.3.3 of the Computer Science Field Guide's algorithms chapter.
2 Picture it
A table of three comparisons that insert 12 into the sorted row 3 8 11 15 20. The 20 is larger than 12, and so is the 15. The 11 is not larger, so the comparisons stop there and 12 goes directly to the right of 11. The last row is marked.
3 Why
Because the row is sorted, every card to the left of a card that is not larger is not larger either. So the first card that is not larger, counting from the right, marks the place: the new card goes directly to its right, and the larger cards it passed stay to the right of it.
The cost depends on where the new card belongs. A new card that is larger than every card takes one comparison, with the last card. A new card smaller than every card takes one comparison for each card in the row. In the example, 12 belongs between 11 and 15 and takes three comparisons.
A card equal to the new one is not larger, so the comparisons stop at it and the new card goes to its right. Equal cards end up in the order in which they were inserted.
4 The rule
With this lesson's steps, a new card goes into a sorted row like this: compare it with the cards from the right-hand end and place it directly to the right of the first card that is not larger, or first in the row when every card is larger.
The row stays sorted. Insertion sort, as the Field Guide describes it, inserts each card in turn into the sorted row so far.
5 How
- Write the sorted row and the new card.
- Compare the new card with the last card, and count 1.
- If the card compared is not larger, place the new card directly to its right: the insertion is done.
- If no card is left to its left, place the new card first: the insertion is done. Otherwise compare with the next card to the left, count 1 and go back to the third step.
- When the insertion is done, check that the new row is sorted and holds every card once.
6 Trace
| Comparison | Card compared | Row | Explanation |
|---|---|---|---|
| 1 | 20 | 3 8 11 15 20 | 20 is larger than 12. Move one card to the left. |
| 2 | 15 | 3 8 11 15 20 | 15 is larger than 12. Move one card to the left. |
| 3 | 11 | 3 8 11 12 15 20 | 11 is not larger than 12, so 12 goes directly to the right of 11. The row is sorted. |
7 The trap
The case with no stopping card needs its own step. Inserting 2 into 4 9 13 passes 13, 9 and 4, all larger, and reaches the left-hand end with no card that is not larger. The step for that case places the new card first, giving 2 4 9 13.
A second slip is placing the new card to the left of the stopping card. Inserting 12 next to 11 that way gives 3 8 12 11 15 20, which is not sorted. The new card goes to the right of the first card that is not larger.
Quick checks
Quick check 1 of 2
Insert the card 2 into the sorted row 4 9 13 with this lesson's steps. How many comparisons are made, and what is the new row?
Show the steps and the answer
3 comparisons are made, and the new row is 2 4 9 13.
Why: By this lesson's steps, 13, 9 and 4 are compared from the right, and all are larger than 2, one comparison each. No card is left, so the 2 goes first.
Quick check 2 of 2
Insert a second 9 into the sorted row 4 9 13 with this lesson's steps. How many comparisons are made, and on which side of the old 9 does the new 9 go?
Show the steps and the answer
2 comparisons are made, and the new 9 goes to the right of the old 9.
Why: By this lesson's steps, 13 is compared first and is larger than 9. The old 9 is not larger than the new 9, so the comparisons stop there and the new card goes directly to its right: 4 9 9 13.
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.