Algorithms and programming · GCSE Computer Science
Bubble sort
GCSE Computer Science bubble sort: compare neighbouring pairs, swap if they are out of order, use a flag or a shrinking tail, and trace a four-item list.
Bubble sort compares neighbours. If they are in the wrong order, swap. Repeat passes until a pass makes no swaps — or until n−1 passes are done.
The important bits
What you need to know
- 1
Bubble sort walks the list comparing each pair of neighbours. If they are out of order, it swaps them, so larger values “bubble” toward the end on an ascending sort.
- 2
One pass is not enough unless the list was already sorted. After pass 1 the largest item is in its final position; after pass 2 the second-largest is, and so on.
- 3
A swapped flag (or equivalent) can stop early: if a pass does no swaps, the list is sorted and further passes are wasted. Mention the flag if the printed algorithm has one.
- 4
Without a flag, a simple version always does n−1 passes for n items. That is easy to trace and inefficient on nearly-sorted data.
- 5
Each pass can ignore the sorted tail: pass k only needs to compare up to index n−k. Forgetting the shrinking bound is a common extra-comparison slip, not usually a wrong final list.
- 6
Time on reverse-sorted data is poor: many swaps, many passes. Space is in-place — a few variables, no extra array. That space point is how you beat merge sort in a comparison.
- 7
Stable versions do not swap equal neighbours, so equal keys keep their original order. GCSE rarely stresses stability; still do not swap when the pair is already in order.
- 8
Trace questions want every swap, not a summary. Write the full list after each swap or after each pass, matching the paper’s instruction.
Quotations worth analysing
Short evidence. Real method.
“Compare each pair of adjacent items and swap if they are in the wrong order.”
Adjacent is the method mark. Swapping distant items is a different sort. “Wrong order” means according to the required ascending or descending rule.
“A pass with no swaps means the list is sorted.”
If the algorithm includes a flag, you must reset it at the start of each pass and stop when it stays false. Ignoring the flag and doing extra passes can still leave the list correct but miss a “how many passes” mark.
“After each pass the next largest item is in its final position.”
This is why later passes can be shorter. It also explains why n−1 passes suffice: the last item has nowhere left to go.
Go deeper
Neighbours only — resist the urge to leap
Bubble sort is taught because you can see it. You never compare item 0 with item 3 in one step; you only ever look at a pair next to each other. If they are out of order you swap, then you move one step along. A 4-item list has three neighbour pairs on a full pass: (0,1), (1,2), (2,3). After that pass the largest value sits at the end, even if the rest is still messy. The next pass can skip that last position. Students who “help” the algorithm by swapping the first and last items are no longer tracing bubble sort. In the exam, freeze the printed pseudocode: if it uses a flag, use the flag; if it always runs to n−1, do not invent an early exit. The paper’s algorithm is the only legal one.
Go deeper
Flag, tail, and why it still loses to merge on large n
A swapped flag turns a nearly-sorted list into a quick finish: one confirming pass and you stop. Reverse-sorted data never gets that gift; you still grind through about n² comparisons. Merge sort is consistently better on large n and needs extra memory for the merged copy. A 6-mark “compare bubble and merge” wants time, space, and behaviour on already-sorted data — not “merge is more advanced”. Bubble is in-place and simple to hand-trace; merge uses more RAM and more lines of code. If the question gives four numbers, they want a pass-by-pass list, not a lecture. Write the array after every swap if the rows are printed that way. Count swaps if asked; a swap is not the same as a comparison. Two items already in order still cost a comparison.
See the idea in action
Bubble sort [5, 1, 4, 2] into ascending order. Neighbour pairs; swap if left > right. Flag starts each pass as false. Pass 1: 5 > 1 swap → [1, 5, 4, 2] 5 > 4 swap → [1, 4, 5, 2] 5 > 2 swap → [1, 4, 2, 5]. Flag true. 5 is now in place. Pass 2 (ignore the last item): 1 < 4 no swap → [1, 4, 2, 5] 4 > 2 swap → [1, 2, 4, 5]. Flag true. 4 is now in place. Pass 3: 1 < 2 no swap → [1, 2, 4, 5]. Flag false. Stop. Sorted list [1, 2, 4, 5]. Three passes, four swaps. A version without a flag would still do a fourth pointless pass on this n = 4 list.
Exam technique
Turn knowledge into marks
Trace the four-item list pass by pass and write the whole array after each swap. If a flag is in the pseudocode, reset it every pass and stop when a pass does no swaps.
Common mistakes
Do not give these marks away
- 01
Forgetting that bubble sort needs another pass after a swap — or ignoring an early-exit flag when the question includes one.
- 02
Comparing non-adjacent items, or writing the sorted list without showing the intermediate swaps the table asked for.
- 03
Claiming bubble sort is “faster” than merge sort because it uses less memory, mixing up time and space.
In bubble sort, when can you be sure the list is sorted and stop extra passes?
AAfter looking at the first pair of items
BWhen a complete pass makes no swaps
CWhen the list has an even length
DAs soon as the smallest item is at the front
Show the answer
When a complete pass makes no swaps. No swaps means every neighbour pair was already in order, so the whole list is sorted. The smallest item reaching the front is not enough; later items can still be out of order.
Quick questions
If this is the bit you searched
How does bubble sort work GCSE Computer Science?
It compares each pair of neighbouring items and swaps them if they are in the wrong order. It repeats passes until the list is sorted, often using a flag to stop when a pass has no swaps.
Why is the largest item at the end after one pass?
On an ascending sort, a larger value keeps swapping right whenever it meets a smaller neighbour, so it bubbles to the end of the unsorted section in that pass.
Is bubble sort efficient?
It is easy to trace but slow on large or reverse-sorted lists because comparisons grow roughly with n². It uses little extra memory. Merge sort is usually faster on large n but needs extra space.
Do I have to swap equal items?
No. If they are already in order (left not greater than right on an ascending sort), leave them. Extra swaps waste work and can change the order of equal keys.