Skip to content

Insertion sort

ordering

Level 1 of 3

Also called Straight insertion.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Insertion sort works on 8, 3, 6, 1, 5. Its first slide places the 3. What is the row after its third slide?

83615
the answer, and why

the answer is A

Slide 1 gives 3, 8, 6, 1, 5. Slide 2 stops the 6 at the 3, giving 3, 6, 8, 1, 5. Slide 3 takes the 1 to the front, giving 1, 3, 6, 8, 5.

if you chose another

  • That is the row after slide 2.
  • That is the row sorted, after slide 4.
  • That stops the 1 at the 3, but the 3 is bigger too.

question 2

How many comparisons does insertion sort make on 2, 5, 1, 4, 3, from start to finish?

25143
the answer, and why

the answer is A

The 5 makes 1. The 1 passes the 5 and the 2: 2, and reaching the front adds none. The 4 passes the 5 and stops at the 2: 2. The 3 passes the 5 and the 4 and stops at the 2: 3. So 1 + 2 + 2 + 3 = 8.

if you chose another

  • That counts the moves.
  • That is the most 5 numbers can cost, 1 + 2 + 3 + 4, on 5, 4, 3, 2, 1.
  • That is the fewest, one for each number placed, on 1, 2, 3, 4, 5.

question 3

A shuffle costs insertion sort about half the moves of a row back to front. Which statement about its moves on average is both true and tight?

the answer, and why

the answer is B

On average n(n−1)4\frac{n(n - 1)}{4}, between an eighth and a quarter of n2n^2 from 2 numbers on. Half of a cost between two fixed multiples of n2n^2 is still between two fixed multiples of n2n^2.

this tests best, worst and average, on the learn page

if you chose another

  • Halving a cost does not change how it grows.
  • True, since the moves are at least n28\frac{n^2}{8}, at least a quarter of nn from 2 numbers on. Not tight: the moves' own floor, n28\frac{n^2}{8}, grows like n2n^2, faster than nn.
  • From 2 numbers on, the moves are at least n28\frac{n^2}{8}, an eighth of nn times nn. That passes 100 times nn once an eighth of nn passes 100. At 800 numbers both are 80,000, so it passes from 801 numbers on, and any fixed multiple of nn likewise.

question 4

Each of these rows of 1 to 5 makes insertion sort move numbers exactly 4 times. On which does it make the most comparisons?

the answer, and why

the answer is C

Each number placed makes one comparison per move, plus one unless it reaches the front. All four rows make 4 moves, so the dearest has nothing reach the front. In 1, 3, 5, 4, 2 the 1 comes first and no slide reaches the front, so 4 moves plus 4 numbers placed, 4 + 4 = 8.

if you chose another

  • The 1 reaches the front, which saves a comparison, so this costs 7.
  • The 2 and the 1 reach the front, which saves two, so this costs 6.
  • The 1 reaches the front, which saves a comparison, so this costs 7.

pen and paper

question 5about 15 minutes

Show that insertion sort makes n(n−1)4\frac{n(n - 1)}{4} moves on average, over every shuffle of nn different numbers.

  1. Say what one move does, and why there is exactly one move for each pair that starts the wrong way round.
  2. Use the average number of such pairs to finish the count.
  3. Check the formula on the 6 shuffles of 1, 2 and 3.
a hint

Look at the two numbers a move swaps, and at which pairs change their order.

a worked answer

A move trades the number being placed with a bigger neighbour. Every other number stands left of both or right of both, so only that wrong pair changes, and is mended. The sorted row has no wrong pair, so the moves are the wrong pairs the row starts with. Each pair is wrong in half the shuffles, so the moves average half of the n(n−1)2\frac{n(n - 1)}{2} pairs, n(n−1)4\frac{n(n - 1)}{4}. For 3 numbers the moves are 0, 1, 1, 2, 2 and 3, which make 9, and 9 over 6 is 1.5.

How many moves does a shuffle take, on average?
  1. the move: see what one move does

    the line:
    a move swaps the numberbeing placed with abigger neighbour\begin{gathered} \text{a move swaps the number} \\ \text{being placed with a} \\ \text{bigger neighbour} \end{gathered}

    why it is allowed: The bigger number steps right as the number being placed steps left. In 3, 8, 5, placing the 5 moves the 8, giving 3, 5, 8.

  2. the move: a move mends one inversion

    the line:
    a move mends oneinversion and makesno new one\begin{gathered} \text{a move mends one} \\ \text{inversion and makes} \\ \text{no new one} \end{gathered}
    pairin 4, 9, 7, 2in 4, 7, 9, 2
    4 and 9rightright
    4 and 7rightright
    4 and 2wrongwrong
    9 and 7wrongright
    9 and 2wrongwrong
    7 and 2wrongwrong
    Every pair, before and after the move of the 9 and the 7. Only that pair changes.

    why it is allowed: The two stand the wrong way round, and the move turns them. Every other number stands left of both or right of both, before and after, so no other pair changes.

  3. the move: count the moves as inversions

    the line:
    the moves are exactly theinversions the row starts with\begin{gathered} \text{the moves are exactly the} \\ \text{inversions the row starts with} \end{gathered}

    why it is allowed: By line 2, and the sorted row has none left. 4, 9, 7, 2 has 4 inversions and takes 4 moves.

  4. the move: count the pairs

    the line:
    a row of n numbers holds1+2+⋯+(n−1) pairs\begin{gathered} \text{a row of } n \text{ numbers holds} \\ 1 + 2 + \dots + (n - 1) \text{ pairs} \end{gathered}

    why it is allowed: A pair is any two numbers. Count each at its second number, place 2 pairing with 1 number, place 3 with 2, up to place nn with n−1n - 1. For 4 numbers, 1 + 2 + 3.

  5. the move: each pair is wrong half the time

    the line:
    each pair stands wrong inexactly half of the shuffles\begin{gathered} \text{each pair stands wrong in} \\ \text{exactly half of the shuffles} \end{gathered}
    pair 1 and 2 wrongtraded, now right
    2, 1, 31, 2, 3
    2, 3, 11, 3, 2
    3, 2, 13, 1, 2
    The 1 and the 2 traded. 3 of the 6 shuffles have that pair wrong, and 3 right.

    why it is allowed: The numbers are all different. Trading a pair's two numbers turns a shuffle with it wrong into one with it right, and back.

  6. the move: count the wrong pairs two ways

    the line:
    adding up the wrong pairsshuffle by shuffle, orpair by pair, givesthe same total\begin{gathered} \text{adding up the wrong pairs} \\ \text{shuffle by shuffle, or} \\ \text{pair by pair, gives} \\ \text{the same total} \end{gathered}
    shuffle1 and 21 and 32 and 3wrong pairs
    1, 2, 30
    1, 3, 2x1
    2, 1, 3x1
    2, 3, 1xx2
    3, 1, 2xx2
    3, 2, 1xxx3
    in all3339
    The 6 shuffles of 1, 2 and 3, an x where a pair stands wrong.

    why it is allowed: Mark an x where a pair is wrong. Adding along the rows or down the columns counts each mark once, so both give 9.

  7. the move: add up the wrong pairs pair by pair

    the line:
    the wrong pairs of everyshuffle, added, come tothe number of pairstimes half the numberof shuffles\begin{gathered} \text{the wrong pairs of every} \\ \text{shuffle, added, come to} \\ \text{the number of pairs} \\ \text{times half the number} \\ \text{of shuffles} \end{gathered}

    why it is allowed: Each column counts the shuffles with its pair wrong, half of them by line 5. For 3 numbers, 3 pairs times 3 is 9.

  8. the move: divide by the shuffles

    the line:
    on average, a shuffleholds half as manywrong pairs as pairs\begin{gathered} \text{on average, a shuffle} \\ \text{holds half as many} \\ \text{wrong pairs as pairs} \end{gathered}

    why it is allowed: Line 7's total over the number of shuffles. For 3 numbers, 9 over 6 is 1.5.

  9. the move: the average moves are the average wrong pairs

    the line:
    on average, a shufflemakes half as manymoves as pairs\begin{gathered} \text{on average, a shuffle} \\ \text{makes half as many} \\ \text{moves as pairs} \end{gathered}

    why it is allowed: By line 3 each shuffle's moves are its wrong pairs, so their averages match, line 8's.

  10. the move: put in the count of pairs

    the line:
    M=12×(1+2+⋯+(n−1))M = \frac{1}{2} \times \bigl(1 + 2 + \dots + (n - 1)\bigr)

    why it is allowed: MM is the moves averaged over every shuffle of nn different numbers. By line 9, half the pairs of line 4.

  11. the move: use the page on adding up, reading its n as n - 1

    the line:
    M=12×(n−1)((n−1)+1)2M = \frac{1}{2} \times \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15.

      why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds nn standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: As in the first ladder's line 7. For 3 numbers, 2 times 3, halved, is 3.

  12. the move: tidy the bracket

    the line:
    M=12×(n−1)n2M = \frac{1}{2} \times \frac{(n - 1)n}{2}

    why it is allowed: One more than n−1n - 1 is nn.

  13. the move: multiply the fractions

    the line:
    M=(n−1)n4M = \frac{(n - 1)n}{4}

    why it is allowed: Half of a half is a quarter. For 3 numbers, 2 times 3 over 4 is 1.5, as in line 8.

  14. the move: write the product the other way round

    the line:
    M=n(n−1)4M = \frac{n(n - 1)}{4}

    why it is allowed: The same product.

  15. the move: multiply out the bracket

    the line:
    M=n2−n4M = \frac{n^2 - n}{4}

    why it is allowed: nn lots of nn make n2n^2, and nn lots of 1 make nn.

  16. the move: take a quarter of each part

    the line:
    M=n24−n4M = \frac{n^2}{4} - \frac{n}{4}

    why it is allowed: A quarter of a difference is the difference of the quarters.

  17. the move: a ceiling

    the line:
    M≤n24M \le \frac{n^2}{4}

    why it is allowed: Line 16 takes something away from n24\frac{n^2}{4}.

  18. the move: read it as a bound

    the line:
    M∈O(n2)M \in O(n^2)

    why it is allowed: By line 17.

  19. the move: a fact for the floor

    the line:
    n4≤n28 for n≥2\frac{n}{4} \le \frac{n^2}{8} \text{ for } n \ge 2
    nnn4\frac{n}{4}n28\frac{n^2}{8}
    20.50.5
    412
    102.512.5
    100251,250
    Equal at 2 numbers, then the eighth of n squared pulls ever further ahead.

    why it is allowed: Times 8, this says twice nn is at most nn times nn, as at the first ladder's line 14.

  20. the move: a floor

    the line:
    M≥n24−n28 for n≥2M \ge \frac{n^2}{4} - \frac{n^2}{8} \text{ for } n \ge 2

    why it is allowed: Line 16 takes away n4\frac{n}{4}, at most line 19's n28\frac{n^2}{8}, and taking away more leaves less. For 10 numbers, 22.5 is at least 25 less 12.5.

  21. the move: tidy the floor

    the line:
    M≥n28 for n≥2M \ge \frac{n^2}{8} \text{ for } n \ge 2

    why it is allowed: A quarter less an eighth is an eighth.

  22. the move: read it as a bound

    the line:
    M∈Ω(n2)M \in \Omega(n^2)
    10210410610810101012101410110210310410510610710110210310410510610710810910101011101210131014101102103104105106107
    • the average moves, n(n−1)4\frac{n(n - 1)}{4}
    • the ceiling of line 17, n24\frac{n^2}{4}
    • the floor of line 21, n28\frac{n^2}{8}
    Log-log, from 10 to ten million numbers. The average moves run between the ceiling and the floor, which stay the same distance apart, the ceiling always twice the floor.

    why it is allowed: By line 21, from 2 numbers on.

  23. the move: a ceiling and a floor together

    the line:
    M∈Θ(n2)M \in \Theta(n^2)

    why it is allowed: Lines 22 and 18 bound MM by the same growth, n2n^2.

try it on small rows: over every order of 3 numbers, the average is M=1.5M = 1.5

this tests where the formula comes from, on the learn page

what a full answer includes
  • a move mends exactly one wrong pair and makes none
  • the sorted row has no wrong pair, so the moves are the wrong pairs the row starts with
  • each pair is wrong in half the shuffles, so the average is half of n(n−1)2\frac{n(n - 1)}{2}
  • 9 moves over the 6 shuffles of 3 numbers, 1.5

question 6about 12 minutes

A list of nn different numbers, at least 3, was in order yesterday, such as 1, 2, 3, 4. Today its last two numbers have traded places, as in 1, 2, 4, 3, and it must be sorted again.

  1. How many comparisons does insertion sort make on today's list?
  2. How many moves does it make on average on a shuffled list? Use question 5.
  3. For 1,000 numbers, compare today's moves with a shuffle's. Is insertion sort a good choice for today's list?
a hint

Ask which numbers have to slide, and how far.

a worked answer

Each number placed before the last stops at its first comparison, n−2n - 2 in all. The last passes the biggest and stops at the next, 2 comparisons and 1 move. That is nn comparisons, 1,000 for 1,000 numbers, and 1 move. A shuffled list makes n(n−1)4\frac{n(n - 1)}{4} moves on average, 249,750 for 1,000, against today's 1. So insertion sort suits today's list well.

What does today's list cost?
  1. the move: place the numbers before the last

    the line:
    the numbers at places2 to n−1 makeone comparison each\begin{gathered} \text{the numbers at places} \\ 2 \text{ to } n - 1 \text{ make} \\ \text{one comparison each} \end{gathered}

    why it is allowed: Places 1 to n−2n - 2 hold yesterday's first numbers in order, and place n−1n - 1 the biggest. Each number at places 2 to n−1n - 1 finds a smaller left neighbour and stops, n−2n - 2 numbers.

  2. the move: place the last number

    the line:
    the last number makes2 comparisons\begin{gathered} \text{the last number makes} \\ \text{2 comparisons} \end{gathered}

    why it is allowed: It is the second biggest. The biggest, on its left, steps right, one comparison and one move. The next number is smaller and stops it, and there is one, since the list holds 3 or more.

  3. the move: add them

    the line:
    C=(n−2)+2C = (n - 2) + 2

    why it is allowed: CC is today's comparisons, by lines 1 and 2. On 1, 2, 4, 3 the 2 and the 4 make one each, and the 3 makes 2.

  4. the move: open the bracket

    the line:
    C=n−2+2C = n - 2 + 2

    why it is allowed: Adding n−2n - 2 is adding nn and taking away 2.

  5. the move: the 2s cancel

    the line:
    C=nC = n

    why it is allowed: Taking away 2 and adding 2 leaves nn. For 4 numbers, 4, as line 3 counts.

try it with a small nn: at n=4n = 4, line 3 gives C=4C = 4, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • each number at places 2 to n−1n - 1 costs one comparison
  • the last number costs 2 comparisons and 1 move
  • the total nn, 1,000 for 1,000 numbers
  • the average moves, n(n−1)4\frac{n(n - 1)}{4} by question 5, 249,750 for 1,000 numbers
  • the choice, by 1 move today against 249,750 on average