Edexcel International Advanced Level Computer Science, point 5.2.4
Definition and function
An insertion sort puts a list in order by dividing it into a sorted part (on the left) and an unsorted part (on the right). It takes each item from the unsorted part and inserts it into its correct place in the sorted part, like sorting a hand of playing cards.
Function: it sorts a list in place. It's simple and works well on small or nearly sorted lists.
Operation
- Treat the first item as a sorted list of one.
- Take the next item (the key) from the unsorted part.
- Compare it with the items in the sorted part, working right to left. Shift each larger item one place right to make a gap.
- Insert the key into the gap.
- Repeat until every item has been inserted.
def insertion_sort(items):
for i in range(1, len(items)):
key = items[i]
j = i - 1
while j >= 0 and items[j] > key:
items[j + 1] = items[j] # shift right
j = j - 1
items[j + 1] = key # insert
return items
It is in place because it rearranges the original list without a second list.
Hand trace: sort [7, 3, 5, 1]
| Step | Key | Shifts | List after (sorted part first) |
|---|---|---|---|
| Start | sorted [7], unsorted [3, 5, 1] |
||
| 1 | 3 | 7 moves right | sorted [3, 7], unsorted [5, 1] |
| 2 | 5 | 7 moves right | sorted [3, 5, 7], unsorted [1] |
| 3 | 1 | 7, 5, 3 move right | sorted [1, 3, 5, 7] |
Detail of step 3 (inserting 1):
j |
items[j] |
items[j] > 1? |
Action |
|---|---|---|---|
| 2 | 7 | Yes | shift 7 right |
| 1 | 5 | Yes | shift 5 right |
| 0 | 3 | Yes | shift 3 right |
| -1 | (loop ends) | place 1 at index 0 |
The final list is [1, 3, 5, 7]. Notice that no swaps are used: items are shifted, and the key is written once.
Efficiency
- Worst case (reverse order): each item is compared with and shifted past every item before it, so about comparisons: time grows with .
- Best case (already sorted): each item is compared once, so only comparisons: time grows in proportion to .
- Space: no extra memory (in place).
- It is usually faster than a bubble sort on the same data, because it makes fewer writes and does less work on nearly sorted data.
Benefits and drawbacks
- Benefits: simple, in place, very quick on nearly sorted or small lists, and it can sort items as they arrive (online).
- Drawbacks: slow on large, random lists (time grows with the square of the size), and lots of shifting if small items are near the end. Better for large data: merge sort and quick sort (15.2).
Insertion sort and bubble sort compared
| Insertion sort | Bubble sort | |
|---|---|---|
| Method | Insert each item into a sorted part | Swap neighbours, bubbling items to the end |
| Sorted part grows from | The left | The right |
| Worst case | ||
| Best case | (with the early exit) | |
| Typical speed | Faster | Slower |
Worked example
Task: show each step of an insertion sort on [9, 4, 6, 2].
| Step | Key | Action | List (sorted part first) |
|---|---|---|---|
| Start | [9] / [4, 6, 2] |
||
| 1 | 4 | shift 9 right, insert 4 | [4, 9] / [6, 2] |
| 2 | 6 | shift 9 right, 6 > 4 so insert after 4 | [4, 6, 9] / [2] |
| 3 | 2 | shift 9, 6, 4 right, insert 2 at the start | [2, 4, 6, 9] |
Shifts made: 1 + 1 + 3 = 5. The final list is [2, 4, 6, 9].
Exam tip: show the sorted part and the unsorted part after each insertion. Say clearly that items are shifted right to make room, and then the key is placed in the gap. For efficiency, mention best case: nearly sorted data is fast.
Check yourself
What are the two parts of the list during an insertion sort?
A sorted part (on the left) and an unsorted part (on the right).
What does the insertion sort do with the key item?
It shifts larger items in the sorted part one place right and inserts the key into the gap.
How many comparisons does an insertion sort make on an already sorted list of 8 items?
7: each of the 7 items after the first is compared once with its left neighbour, and needs no shift.
What is the list [3, 8, 5, 1] after inserting the third item (5)?
[3, 5, 8, 1].
Why is "in place" an advantage?
It sorts within the original list and needs no extra memory for a second copy.