2. Bubble Sort
1What is a sorting algorithm?
A sorting algorithm is a set of step-by-step instructions that rearranges data into a specific order — usually numerical (smallest to largest) or alphabetical (A to Z).
2What is a bubble sort?
Think of a bubble sort like a line of people standing in a random order of height, and you (the "algorithm") walk down the line comparing each pair of neighbours. If the person on the left is taller than the person on the right, you swap them. You keep walking to the end of the line — that's one full pass. Then you walk the whole line again, and again, until you make a full pass with zero swaps — meaning everyone is finally in order.
The name "bubble sort" comes from the fact that on each pass, the largest unsorted value "bubbles up" to its correct position at the end — a bit like an air bubble rising to the top of water.
The Rule
1. Compare the first pair.
2. IF out of order → swap them.
3. Move to the next pair, repeat until the end (this is one pass).
4. IF any swap happened in that pass → do another pass.
5. ELSE (no swaps at all) → STOP, it's sorted.
Let's trace Pass 1 on the dataset [9, 2, 4, 7, 10, 3, 1] from the textbook, comparing neighbours left to right:
| Comparison | Result | List after step |
| 9 vs 2 | ✓ SWAP | 2, 9, 4, 7, 10, 3, 1 |
| 9 vs 4 | ✓ SWAP | 2, 4, 9, 7, 10, 3, 1 |
| 9 vs 7 | ✓ SWAP | 2, 4, 7, 9, 10, 3, 1 |
| 9 vs 10 | ✗ no swap | 2, 4, 7, 9, 10, 3, 1 |
| 10 vs 3 | ✓ SWAP | 2, 4, 7, 9, 3, 10, 1 |
| 10 vs 1 | ✓ SWAP | 2, 4, 7, 9, 3, 1, 10 |
See how 10, the largest number, "bubbled" all the way to the end of the list in just one pass? That always happens — the biggest unsorted value reaches its final position by the end of every pass. That's why, on the next pass, you technically don't even need to re-check the last element again (though you don't have to worry about that detail for the exam).
Examiner Tip
You do NOT need to write out every single comparison in an exam answer. Showing the state of the list at the end of each pass is enough to get full marks, as long as the end result is correctly sorted through legitimate passes.
Here's the full pseudocode, using a swaps flag exactly like the found flag in linear search:
DECLARE nums : ARRAY[1:11] OF INTEGER
nums ← [66, 7, 69, 50, 42, 80, 71, 321, 67, 8, 39]
DECLARE numlength : INTEGER
numlength ← 11
DECLARE swaps : BOOLEAN
swaps ← TRUE
WHILE swaps = TRUE
swaps ← FALSE
FOR y ← 1 TO numlength - 1
IF nums[y] > nums[y + 1] THEN
DECLARE temp : INTEGER
temp ← nums[y]
nums[y] ← nums[y + 1]
nums[y + 1] ← temp
swaps ← TRUE // a swap was made
ENDIF
NEXT y
numlength ← numlength - 1 // last value now sorted, shrink range
ENDWHILE
Why the temp variable?
To swap two values in most languages you can't just say nums[y] ← nums[y+1] — that would overwrite nums[y] before you've saved its original value anywhere. So you store it in a temporary variable first: save the original left value into temp, overwrite the left value with the right value, then put temp into the right-hand slot. (Python can skip this using a, b = b, a, but pseudocode and most other languages need the temp variable.)
Practice Question 3
Perform a bubble sort on the list [8, 3, 5, 1]. Show the state of the list at the end of each pass.
Practice Question 4
A student says: "A bubble sort always needs exactly as many passes as there are items in the list." Is this true? Explain your reasoning using the algorithm's actual stopping condition.