A Level · Topic 19.1
A-Level Computer Science 9618 / Topic 19 / A2

Slide it into place.

An insertion sort builds a sorted part at the front of the list, taking the next item each time and sliding it back to where it belongs. You write and trace it in pseudocode, then compare it with the bubble sort for different starting orders and sizes of data.

The key idea

An insertion sort treats the first element as a sorted part of length one. Each pass takes the next element as the key, compares it with the sorted part from right to left, shifts every larger element one place to the right, and drops the key into the gap. After pass i the first i elements are in order. On data that is already sorted each pass makes one comparison, so it needs only n − 1 comparisons; on data in reverse order it needs n(n − 1)/2. The bubble sort has the same best and worst cases, but usually makes more comparisons and moves data with three-step swaps.

Insertion sort of 27, 12, 35, 8, 19: the sorted part grows by one each pass start 271235819 i = 2, Key = 12 122735819 1 comparison, 1 shift i = 3, Key = 35 122735819 1 comparison, 0 shifts i = 4, Key = 8 812273519 3 comparisons, 3 shifts i = 5, Key = 19 812192735 3 comparisons, 2 shifts sorted partwhere the key was inserted
Fig. 1: an insertion sort of five integers, shown after each pass; 8 comparisons in total
Section 01

Build a sorted part.

In the lab, step through an insertion sort. Each pass lifts out one item (the key), shifts larger items in the sorted part one place right, and places the key in the gap. Count the comparisons on random data, then on sorted data.

Worked example One pass in detail
After two passes, Nums holds 12, 27, 35, 8, 19 (Fig. 1). Describe pass 3, which inserts the key 8.
  1. Key ← 8, the element in position 4. The sorted part is 12, 27, 35.
  2. 35 > 8: shift 35 into position 4. 27 > 8: shift 27 into position 3. 12 > 8: shift 12 into position 2.
  3. The start of the list has been reached, so the loop stops: 3 comparisons and 3 shifts.
  4. Write the key into position 1.
Answer: Nums holds 8, 12, 27, 35, 19; the first four elements are now sorted
Your turn
Do the final pass: insert the key 19 into 8, 12, 27, 35. How many comparisons are made?
Examiner trap

Do not describe an insertion sort as "comparing adjacent pairs and swapping them": that is a bubble sort. Say that each item is inserted into its correct position in the sorted part.

Section 02

Write it in pseudocode.

The outer FOR loop chooses the key; the inner WHILE loop shifts larger items right. Learn the line List[j + 1] ← Key at the end: it is the mark examiners look for.

DECLARE List : ARRAY[1:6] OF INTEGER
DECLARE i : INTEGER
DECLARE j : INTEGER
DECLARE Key : INTEGER
FOR i ← 2 TO 6
    Key ← List[i]
    j ← i - 1
    WHILE j >= 1 AND List[j] > Key
        List[j + 1] ← List[j]
        j ← j - 1
    ENDWHILE
    List[j + 1] ← Key
NEXT i
Worked example Full trace
List holds 14, 6, 22, 3, 17, 9. Show the list after each pass of the insertion sort and count the comparisons (evaluations of List[j] > Key).
  1. i = 2, Key 6: 14 > 6 shift; j = 0 so stop. 1 comparison: 6, 14, 22, 3, 17, 9.
  2. i = 3, Key 22: 14 > 22 FALSE. 1 comparison: no change.
  3. i = 4, Key 3: 22, 14 and 6 are all larger and shift; j = 0. 3 comparisons: 3, 6, 14, 22, 17, 9.
  4. i = 5, Key 17: 22 shifts, 14 > 17 FALSE. 2 comparisons: 3, 6, 14, 17, 22, 9.
  5. i = 6, Key 9: 22, 17 and 14 shift, 6 > 9 FALSE. 4 comparisons: 3, 6, 9, 14, 17, 22.
Answer:
iKeyList after the passComparisons
266, 14, 22, 3, 17, 91
3226, 14, 22, 3, 17, 91
433, 6, 14, 22, 17, 93
5173, 6, 14, 17, 22, 92
693, 6, 9, 14, 17, 224

Total: 11 comparisons and 8 shifts.

Your turn
A 4-element array holds 4, 9, 2, 7. What does it hold after the pass with i = 3, and how many comparisons does the whole sort make?
Examiner trap

Writing List[j] ← Key after the loop puts the key one place too far left. When the loop stops, j points at the element that is not larger, so the gap is at j + 1.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

In the lab, which element becomes the key on the first pass of an insertion sort?

Quick check+10 XP

During one pass, what happens to each element of the sorted part that is larger than the key?

Quick check+10 XP

Why is Key ← List[i] done before the WHILE loop starts?

Quick check+10 XP

An insertion sort is run on 8 items that are already in order. How many comparisons does it make?

Section 03

Which sort, and when?

The syllabus says the performance of a sorting routine may depend on the initial order of the data and the number of items. In the lab, compare the two sorts on random, sorted and reversed data and watch how the counts grow with n.

Starting order (n items)Insertion sort comparisonsImproved bubble sort comparisons
Already sorted (best case)n − 1n − 1 (one pass, no swaps, stop)
Reverse order (worst case)n(n − 1)/2n(n − 1)/2
27, 12, 35, 8, 19 (n = 5)810
Reverse order, n = 10049504950
Worked example Predict the work
An insertion sort takes 0.2 seconds to sort 2000 records that are in reverse order. Estimate the time for 6000 records in reverse order, and for 6000 records that are already sorted.
  1. In the worst case the work grows with n2. 6000 is 3 times 2000, so the work is about 32 = 9 times as much.
  2. Reverse order: 0.2 × 9 = 1.8 seconds.
  3. Already sorted: only n − 1 comparisons, growing with n, so the time is tiny compared with the worst case.
Answer: About 1.8 s in reverse order; a small fraction of a second when already sorted
Your turn
How many comparisons does an insertion sort make on 10 items in reverse order? And on 10 items already sorted?
Examiner trap

"Insertion sort is faster than bubble sort" earns little on its own. Link the answer to the data: for nearly sorted data both are quick, and in the worst case both need n(n − 1)/2 comparisons.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to A Level Paper 3 and Paper 4 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

How many comparisons does an insertion sort make on 6 items in reverse order?

Exam style+20 XP

For which data is an insertion sort most efficient?

Exam style+20 XP

Which line places the key after the inner WHILE loop has finished?

Exam style+20 XP

A sort takes 1 second for 1000 items in the worst case and is O(n2). About how long for 4000 items in the worst case?

Skill unlocked

Insertion sort, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Writing and tracing an insertion sort, and comparing it with the bubble sort for different initial orders and sizes of data.