Insertion sort

Computer Science · Algorithms

Question 1 of 10

0

    Insertion sort (ascending) on [26, 17, 9, 19, 28]. Step i (i = 1, 2, ...) takes the item at index i and moves it left into the sorted part at the front. A shift moves one larger item one place to the right. A comparison is each test of the item on the left against the item being inserted, made only while there is still an item on the left. How many shifts does step 1 make?

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Insertion sort explained

    Insertion sort builds a sorted list one item at a time. It takes the next unsorted item and inserts it into the correct place among the items that are already sorted, much as many people sort a hand of playing cards.

    The first item counts as sorted on its own. Then, for each remaining item:

    1. Compare it with the sorted items, starting from the one nearest to it and moving left.
    2. Shift each larger item one place to the right to make a gap.
    3. Place the item in the gap, so the sorted part has one more item.

    In the worst case, the list is in reverse order and every item must move past all the items before it, giving about n22\frac{n^2}{2} comparisons, which is O(n2)O(n^2). If the list is already sorted, each item needs only one comparison, so the time is O(n)O(n).

    A common mistake is to overwrite the item being inserted. Store it in a temporary variable before shifting the larger items, or it is lost.

    Worked example

    Sort [5, 2, 4, 1] into ascending order.

    1. Insert 2: 5 is larger, so shift it right and place 2 at the front. The list is [2, 5, 4, 1]. This takes 1 comparison and 1 shift.
    2. Insert 4: compare with 5, which is larger, so shift it. Compare with 2, which is smaller, so place 4 after it. The list is [2, 4, 5, 1]. This takes 2 comparisons and 1 shift.
    3. Insert 1: shift 5, 4 and 2 to the right, then place 1 at the front. The list is [1, 2, 4, 5]. This takes 3 comparisons and 3 shifts.
    4. In total there are 6 comparisons and 5 shifts.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      Insertion sort (ascending) on [2, 28, 25, 16, 11]. Step i (i = 1, 2, ...) takes the item at index i and moves it left into the sorted part at the front. A shift moves one larger item one place to the right. A comparison is each test of the item on the left against the item being inserted, made only while there is still an item on the left. After step 3, which positions hold the sorted part?

      • The last 4 positions
      • Only index 3
      • The first 4 positions (indices 0 to 3)
      • Only index 0
    • Medium

      Insertion sort (ascending) on [3, 18, 11, 32, 40]. Step i (i = 1, 2, ...) takes the item at index i and moves it left into the sorted part at the front. A shift moves one larger item one place to the right. A comparison is each test of the item on the left against the item being inserted, made only while there is still an item on the left. What is the value at index 0 (counting from 0) after step 4?

    • Hard

      Insertion sort (ascending) on [19, 12, 39, 34, 15, 20]. Step i (i = 1, 2, ...) takes the item at index i and moves it left into the sorted part at the front. A shift moves one larger item one place to the right. A comparison is each test of the item on the left against the item being inserted, made only while there is still an item on the left. How many shifts are made in steps 1 to 2 in total?