Sorting is one of the most fundamental tasks in computing — putting a list of items into order is something programs do millions of times a day. At KS3, students learn two classic sorting algorithms: bubble sort and insertion sort. Understanding how each works, and how efficient each is, develops the algorithmic thinking examiners are looking for.
What is a sorting algorithm and why does it matter?
A sorting algorithm is a systematic method for arranging items in a list into a defined order — most commonly ascending (smallest to largest) or alphabetical. Sorting underlies an enormous number of everyday computing tasks: displaying search results, organising a contact list, preparing data for a binary search, and ranking leaderboards, to name just a few.
Different algorithms take different amounts of time and use different amounts of memory to sort the same list. For small lists, any algorithm will do. For millions of items, a poorly chosen algorithm could take hours instead of seconds. At KS3, the focus is on understanding how two simple algorithms work, not on implementing them in production code.
How does bubble sort work?
Bubble sort works by repeatedly stepping through the list, comparing adjacent pairs of items and swapping them if they are in the wrong order. The largest unsorted item "bubbles up" to its correct position at the end of the list with each pass.
Bubble sort — worked example
Starting list: [5, 3, 8, 1, 4]
Pass 1:
| Step | Comparison | Action | List after step |
|---|---|---|---|
| 1 | 5 vs 3 | Swap (5 > 3) | [3, 5, 8, 1, 4] |
| 2 | 5 vs 8 | No swap | [3, 5, 8, 1, 4] |
| 3 | 8 vs 1 | Swap (8 > 1) | [3, 5, 1, 8, 4] |
| 4 | 8 vs 4 | Swap (8 > 4) | [3, 5, 1, 4, 8] |
After pass 1, the largest value (8) is in its final position. After each subsequent pass, the next-largest value reaches its final position. For a list of n items, at most n − 1 passes are needed.
Pseudocode:
FOR pass FROM 1 TO length(list) - 1
FOR i FROM 0 TO length(list) - pass - 1
IF list[i] > list[i + 1] THEN
temp = list[i]
list[i] = list[i + 1]
list[i + 1] = temp
ENDIF
ENDFOR
ENDFOR
The swap uses a temporary variable (temp) so that the value of list[i] is not lost before it is copied to list[i + 1].
How does insertion sort work?
Insertion sort works differently: it builds a sorted section at the beginning of the list, one item at a time. It picks up each unsorted item in turn and inserts it into the correct position within the already-sorted section — the way a card player sorts a hand of cards.
Insertion sort — worked example
Starting list: [5, 3, 8, 1, 4]
| Pass | Item taken | Sorted section before insertion | After insertion |
|---|---|---|---|
| 1 | 3 | [5] |
[3, 5] |
| 2 | 8 | [3, 5] |
[3, 5, 8] |
| 3 | 1 | [3, 5, 8] |
[1, 3, 5, 8] |
| 4 | 4 | [1, 3, 5, 8] |
[1, 3, 4, 5, 8] |
In pass 3, the value 1 is compared with 8 (swap), then 5 (swap), then 3 (swap), then 3 > 1 so it stops and 1 is placed at position 0. The sorted section grows by one item with each pass.
Pseudocode:
FOR i FROM 1 TO length(list) - 1
key = list[i]
j = i - 1
WHILE j >= 0 AND list[j] > key DO
list[j + 1] = list[j]
j = j - 1
ENDWHILE
list[j + 1] = key
ENDFOR
How efficient are bubble sort and insertion sort?
Both algorithms are considered inefficient for large lists because, in the worst case, the number of comparisons grows with the square of the list length (n²). Doubling the list size quadruples the work.
| Feature | Bubble sort | Insertion sort |
|---|---|---|
| Worst-case comparisons | O(n²) | O(n²) |
| Best case (already sorted) | O(n) with optimisation | O(n) |
| Memory use | In-place (no extra list needed) | In-place |
| Stable? (equal items keep their order) | Yes | Yes |
| Easy to understand? | Very | Moderate |
| Practical use for large data? | Rarely | Occasionally (nearly-sorted data) |
In practice, for large datasets, faster algorithms such as merge sort (O(n log n)) are used. But bubble sort and insertion sort are ideal for teaching because their logic can be traced step by step without a computer.
How do sorting algorithms connect to other KS3 topics?
Sorting algorithms are a direct application of computational thinking — they decompose the task of sorting into repeatable steps and use abstraction to focus on the comparison and swap operations. They also connect to:
- Iteration — both algorithms use nested loops to repeat comparisons
- Variables and assignment — the swap requires a temporary variable
- Algorithms and flowcharts — sorting algorithms can be expressed as pseudocode or flowcharts
- Searching — binary search requires a sorted list to work
The DfE curriculum requires students to understand algorithms including sorting and searching (gov.uk/government/publications/national-curriculum-in-england-computing-programmes-of-study), and both AQA and OCR GCSE specifications include trace tables for sorting algorithms as an exam question type.
Frequently asked questions
What is bubble sort in simple terms for KS3?
Bubble sort repeatedly compares neighbouring pairs of items in a list and swaps them if they are in the wrong order. After each full pass through the list, the largest remaining unsorted item has moved to its correct position at the end. The process repeats until no more swaps are needed, at which point the list is fully sorted.
What is insertion sort and how is it different from bubble sort?
Insertion sort builds a sorted section at the start of the list, one item at a time. It picks up each new item and slides it into the correct position within the already-sorted section, shifting larger items one place to the right to make room. The key difference is that insertion sort maintains a growing sorted section rather than repeatedly bubbling the largest value to the end.
Why do we need a temporary variable in a swap?
When swapping two variables a and b, you cannot simply write a = b then b = a — the first line overwrites a's original value before you can copy it anywhere. A temporary variable (temp = a; a = b; b = temp) preserves a's original value so it can be placed into b. This three-step swap is a standard pattern that appears in nearly all sorting algorithms.
Which sorting algorithm is better at KS3 level?
For most exam questions they are equivalent — both have O(n²) worst-case behaviour. However, insertion sort is generally faster in practice for nearly-sorted data, and it is the algorithm card players instinctively use. Bubble sort is often taught first because each step (compare and swap adjacent pair) is very easy to visualise. For GCSE exams, focus on being able to trace both algorithms through a short list and identify how many passes or comparisons are needed.
For Socratic computing tutoring at KS3 and GCSE — see aitutors.me.