Skip to content

Insertion sort

ordering

Level 1 of 3

Also called Straight insertion.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from insertion sort's own run on the numbers shown.

choose one

question 1

Insertion sort works on 3, 7, 9, 5, 2. Its first slide places the 7. What is the row after its third slide?

37952
the answer, and why

the answer is C

After slides 1 and 2 the row is 3, 7, 9, 5, 2. Slide 3 takes the 5 left past every bigger number before it: 3, 5, 7, 9, 2.

if you chose another

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

new numbersthe test page's numbers

question 2

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

48329
the answer, and why

the answer is D

Each number placed costs one comparison for each bigger number it passes, and one more unless it reaches the front: 1 + 2 + 3 + 1, 7 in all.

if you chose another

  • That counts the moves.
  • That is the most 5 numbers can cost, 1 + 2 + 3 + 4, on a row back to front.
  • That is the fewest, one for each number placed, on a row in order.

new numbersthe test page's numbers

question 3

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 A

Insertion sort makes 8 comparisons on 1, 5, 3, 2, 4, the most of the four.

if you chose another

  • Insertion sort makes 7 comparisons on this row, fewer than 8.
  • Insertion sort makes 7 comparisons on this row, fewer than 8.
  • Insertion sort makes 7 comparisons on this row, fewer than 8.

new numbersthe test page's numbers