Quarry School

Insert one card into sorted order

Subject: Computing
Level 1 · FoundationsAbout 6 min read2 new terms
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    A library buys one new book.

    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.

    Step

    1. Write the sorted row and the new card.

    2. Compare the new card with the last card, and count 1.

    3. If the card compared is not larger, place the new card directly to its right: the insertion is done.

    4. 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.

    5. When the insertion is done, check that the new row is sorted and holds every card once.

    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.

    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

    Compare 12 withLarger than 12?20yes15yes11no: stop

    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.

    Inserting 12 into the sorted row 3 8 11 15 20

    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

    1. Write the sorted row and the new card.
    2. Compare the new card with the last card, and count 1.
    3. If the card compared is not larger, place the new card directly to its right: the insertion is done.
    4. 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.
    5. When the insertion is done, check that the new row is sorted and holds every card once.

    6 Trace

    Trace the insertion of 12 into the row 3 8 11 15 20 with this lesson's steps
    ComparisonCard comparedRowExplanation
    1203 8 11 15 2020 is larger than 12. Move one card to the left.
    2153 8 11 15 2015 is larger than 12. Move one card to the left.
    3113 8 11 12 15 2011 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
    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.

    Source: University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.3 Sorting

    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
    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.

    Source: University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.3 Sorting

    Sources

    1. 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.