Insert one card into sorted order — quick review
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.