15.1 What is Insertion Sort?
Insertion sort builds a sorted region one element at a time. It assumes the first element is already sorted, then takes each subsequent element (the key), compares it with the sorted part to its left, shifts larger elements right to make space, and inserts the key into its correct position.
- 1. Start with the second element. Assume the first element is already sorted.
- 2. Compare the key with elements to its left. Find its correct position in the sorted part.
- 3. Shift larger elements right. Move elements that are greater than key one position to the right.
- 4. Insert the key. Place it in the correct position. Repeat for all elements.
Example: sort [7, 3, 5, 2]
# Start: [7, 3, 5, 2] (7 is the sorted region)
# Step 1: key = 3. Compare with 7. 3 < 7, so shift 7 right.
# [7, 7, 5, 2] -> insert 3 at position 0
# [3, 7, 5, 2] sorted part: [3, 7]
# Step 2: key = 5. Compare with 7. 5 < 7, shift 7 right.
# Compare with 3. 3 < 5, so stop. Insert 5 at position 1.
# [3, 5, 7, 2] sorted part: [3, 5, 7]
# Step 3: key = 2. Compare with 7, 5, 3 — all > 2, shift all right.
# Insert 2 at position 0.
# [2, 3, 5, 7] sorted part: [2, 3, 5, 7] — DONE!- In-place algorithm: It does not require extra space; sorting happens within the original list.
- Best for small data sets: Efficient for small or nearly sorted datasets.
- Time complexity: O(n²) worst case, O(n) best case (already sorted).
How Insertion Sort Works
15.1B Interactive Simulator
Try insertion sort yourself. Change the array values, then click Start. Use Step for manual control or Play for auto-run. Watch the key (amber) being compared with sorted elements (rose), shifted right, and inserted into its correct position.
Simulation
Status: Click Start to begin.
Code
1for i in range(1, len(arr)):2 key = arr[i]3 while j >= 0 and arr[j] > key:4 arr[j+1] = arr[j]5 j -= 16 arr[j+1] = key15.2 Insertion Sort in Python & Pseudocode
Python
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
# Shift elements greater than key to the right
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key # Insert key
# Example
arr = [8, 3, 5, 2]
insertion_sort(arr)
print("Sorted array:", arr)
# Output: [2, 3, 5, 8]Cambridge pseudocode
PROCEDURE InsertionSort(A : ARRAY OF INTEGER)
FOR i <- 1 TO LENGTH(A) - 1
key <- A[i]
j <- i - 1
WHILE j >= 0 AND A[j] > key DO
A[j + 1] <- A[j]
j <- j - 1
ENDWHILE
A[j + 1] <- key
NEXT i
ENDPROCEDURE- The outer loop starts at
i = 1(not 0) — the first element is already sorted. key = arr[i]saves the value being inserted before shifting overwrites it.j = i - 1starts the comparison at the element just left of key.- The while loop shifts elements greater than key one position right.
- After the loop, insert key at
arr[j + 1]— j has been decremented past the insertion point.
- Step 1: key=1, compare with 9, shift 9 right, insert 1 at 0
- Step 2: key=6, compare with 9, shift 9 right, 1<6 so insert 6 at 1
- Step 3: key=3, compare with 9,6,1, shift all right, insert 3 at 1
Python & Pseudocode Implementation
15.3 Tracing Insertion Sort
A trace table for insertion sort tracks: Step, i, key, j, the comparison, and the array after the inner loop. Here is the trace for arr = [8, 3, 5, 2]:
| Step | i | key | j | Comparison | Array after inner loop |
|---|---|---|---|---|---|
| 1 | 1 | 3 | 0 | 8 > 3, shift 8 right | [3, 8, 5, 2] |
| 2 | 2 | 5 | 1 | 8 > 5, shift 8 right; 3 < 5, stop | [3, 5, 8, 2] |
| 3 | 3 | 2 | 2→1→0→-1 | 8>2, 5>2, 3>2, shift all right | [2, 3, 5, 8] |
- In a trace, show the full array state after each step (not each individual shift).
- The examiner checks that the sorted region grows correctly from left to right.
- Each step should show the key, the comparisons made, and the final array state.
- Step 1: i=1, key=5, j=0, 9>5 shift, insert at 0
- Step 2: i=2, key=7, j=1, 9>7 shift, 5<7 stop, insert at 1
- Step 3: i=3, key=2, shift 9,7,5 right, insert at 0
- Step 4: i=4, key=4, shift 9,7,5 right, 2<4 stop, insert at 1
- 6 steps (i from 1 to 6)
- Step 1: key=7, compare with 12, shift 12, insert 7 at 0
- Step 2: key=10, compare with 12, shift 12, 7<10 stop, insert at 1
- Continue for all 6 steps
Tracing Insertion Sort
15.4 Performance & Comparison
| Scenario | Comparisons | Shifts | Big O |
|---|---|---|---|
| Best case (already sorted) | n - 1 | 0 | O(n) |
| Worst case (reverse sorted) | ~n²/2 | ~n²/2 | O(n²) |
| Average case | ~n²/4 | ~n²/4 | O(n²) |
- Insertion sort is in-place (O(1) extra space), stable (equal elements keep their relative order), and adaptive (nearly-sorted data runs close to O(n)).
- It is preferred for small or nearly-sorted datasets.
- Worst case is O(n²) — comparisons and shifts grow quadratically
- Merge sort and quick sort are O(n log n) — much faster for large n
- For large datasets, the number of shifts becomes very large
- Small lists (overhead of complex algorithms is not worth it)
- Nearly-sorted data (best case is O(n))
- Simple to implement, minimal memory, real-time systems
Performance & Comparison
15.5 Debugging a Buggy Version
Below is a pseudocode implementation of insertion sort. Each line is numbered. There are four errors that will cause incorrect sorting. Find each one.
01 PROCEDURE InsertionSort(A : ARRAY OF INTEGER)
02 FOR i <- 0 TO LENGTH(A) DO // ERROR 1
03 key <- A[i]
04 j <- i // ERROR 2
05 WHILE j >= 0 AND A[j - 1] < key DO // ERROR 3
06 A[j] <- A[j + 1]
07 j <- j + 1
08 ENDWHILE
09 A[j + 1] <- key // ERROR 4
10 ENDFOR
11 ENDPROCEDURE- Line 2: starts at 0, should start at 1
- Line 4: j = i, should be j = i - 1
- Line 5: uses < (wrong direction), should be >
- Line 9: A[j+1] = key, should be A[j] = key (in this buggy context)
Debugging & Common Errors
15.6 Full Exam-Style Question
- Assume the first element is sorted
- Take each element (key) and compare with the sorted part to its left
- Shift larger elements right to make space
- Insert key in its correct position
- Repeat for all elements
insertion_sort(arr) that sorts the list in ascending order using insertion sort and returns the sorted list.- Outer loop: for i in range(1, len(arr))
- key = arr[i], j = i - 1
- while j >= 0 and arr[j] > key: shift right
- arr[j + 1] = key
- Return arr
- Nearly-sorted data: few elements out of place
- The while loop condition fails quickly (arr[j] < key)
- Few shifts needed, so close to O(n)
Key Points Recap
✓ Key Points Summary
15.7 Practice Tasks
Fifteen exam-style insertion sort tasks. Each shows only the question — click Hint for the thought process, or Help for the worked solution.
- For every insertion sort task, the marker checks: (1) outer loop starts at 1 (not 0), (2) key is saved before shifting, (3) while loop shifts elements > key right (not swaps), (4) key is inserted at j+1 after the loop.
- The most common mark loss is starting the loop at 0 instead of 1.
Question Bank
Answer all questions, then press Submit Quiz to see your score.
Question 1Multiple Choice
What is the key idea of insertion sort?
Question 2True / False
The outer loop starts from index 1 because index 0 is already sorted.
Question 3Multiple Choice
What does the while loop do?
Question 4Multiple Choice
Where is key inserted after the while loop?
Question 5True / False
Insertion sort is an in-place algorithm.
Question 6Multiple Choice
What is the worst-case Big O?
Question 7Multiple Choice
What is the best case?
Question 8True / False
Insertion sort is preferred for small or nearly-sorted datasets.
Question 9Multiple Choice
In the pseudocode, the outer loop is:
Question 10Multiple Choice
What is the while loop condition?
Question 11True / False
For descending order, change > to < in the while condition.
Question 12Multiple Choice
Both bubble sort and insertion sort are O(n²). Which is faster on nearly-sorted data?
Answer all 12 questions to enable submission.