← IGCSE Computer Science Paper 2

Bubble sort: how it works

A sorting method that compares neighbouring items and swaps them if in the wrong order, repeating passes until the list is sorted. The largest value bubbles to the end each pass.

Understanding Bubble sort: how it works in IGCSE Computer Science Paper 2

What it does: A FOR loop steps through the neighbouring pairs in one pass. With 5 items there are 4 pairs, so i runs from 1 to 4. Descending order puts the largest values first. So a pair is in the wrong order if the left value is smaller than the right one, and the comparison uses < instead of >. A bubble sort always compares two items that sit side by side, then moves along one position and compares the next pair. Each pass fixes the next largest value at the end. A list of n items may need several passes before every item is in place.

Common mistakes: Line 3 should be A[i] ← A[i + 1]. A swap needs Temp to hold the first value, then the second value moves into position i, then Temp goes into position i + 1. As written, A[i] gets its own value back and the value in A[i + 1] is never moved. For ascending order, neighbours are in the wrong order when the first is bigger than the second, so the test must be A[i] > A[i + 1]. Using < swaps pairs that are already correct.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 296–306) and word search (Modules 296–316).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
In a bubble sort, which items are compared with each other?
Neighbouring items (next to each other in the list)The first item and the last item only
Matches
[5, 3, 8] after one pass becomes [3, 5, 8] and the sort is always finished
↓
One pass only guarantees that the largest value is at the end. More passes are usually needed, so the sort is not necessarily finished.
Applying
Meanings
Term
bubble sort
Meaning
A sorting method that compares neighbouring items and swaps them if in the wrong order, repeating passes until the list is sorted. The largest value bubbles to the end each pass.
Practice
FOR i ← 1 TO 4
Testing
Gaps
IF Numbers[i] Numbers[i + 1] THEN
Check Answer
Spots
2 Temp ← A[i]
3 A[i] ← Temp

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Puffin' badge — Definition Diver, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. In a bubble sort, which items are compared with each other?
A. The first item and the last item only
B. Neighbouring items (next to each other in the list) ✓
A bubble sort always compares two items that sit side by side, then moves along one position and compares the next pair.
2. Sorting in ascending order, a bubble sort compares 7 and 3. What happens?
A. They are swapped, because 7 is bigger than 3 ✓
B. They stay where they are, because 7 comes first
For ascending order (smallest to largest), a pair is swapped when the left item is greater than the right item.
3. After the first full pass of a bubble sort in ascending order, where is the largest value?
A. At the start of the list
B. At the end of the list ✓
The largest value 'bubbles' to the end during the first pass, because it keeps getting swapped past every smaller neighbour.
4. After each pass of a bubble sort, how many more items are in their final position?
A. At least one more item is in place at the end ✓
B. The whole list is always sorted after one pass
Each pass fixes the next largest value at the end. A list of n items may need several passes before every item is in place.
5. List: 4, 9, 2. Which pair of values is compared first in a bubble sort?
A. 4 and 9 ✓
B. 4 and 2
Bubble sort starts at the beginning of the list and compares the first two neighbours: 4 and 9. Then it compares 9 and 2.

Match the Pairs

IF Numbers[i] > Numbers[i + 1] THEN → Compares two neighbouring items and checks whether they are in the wrong order for an ascending sort
Temp ← Numbers[i] → Stores one of the two items safely so the pair can be swapped without losing a value
Each pass of a bubble sort compares neighbours from the start of the list up to the last item not yet in place → FOR i ← 1 TO 4
To sort in descending order, swap when the left item is smaller than the right item → IF Scores[i] < Scores[i + 1] THEN
[5, 3, 8] after one pass becomes [3, 5, 8] and the sort is always finished → One pass only guarantees that the largest value is at the end. More passes are usually needed, so the sort is not necessarily finished.
Names: Zara, Adam, Maya. Bubble sort cannot sort text, only numbers → A bubble sort can sort strings as well as numbers, because strings can be compared in alphabetical order.

Key Term

bubble sort
A sorting method that compares neighbouring items and swaps them if in the wrong order, repeating passes until the list is sorted. The largest value bubbles to the end each pass.

Fill in the Blanks

1. IF Numbers[i] > Numbers[i + 1] THEN
Hint: Type the comparison operator that is true when the left item is larger than its right neighbour, so the pair is in the wrong order for ascending sort.
2. Temp ← Numbers[i] Numbers[i] ← Numbers[i + 1] Numbers[i + 1] ← Temp
Hint: Type the temporary variable that holds the first value so it is not lost during the swap.
3. IF Numbers[i] < Numbers[i + 1] THEN
Hint: Type the comparison operator that finds a wrong-order pair when sorting into descending order.
4. FOR Pass ← 1 TO 4 FOR i ← 1 TO 5 - Pass
Hint: Type the outer loop variable that counts passes, so each pass checks one fewer item as the largest values settle at the end.
5. IF Names[i] > Names[i + 1] THEN
Hint: Type the index of the neighbouring item that is compared with the current string.

Spot the Error

Question 1
A IF A[i] > A[i + 1] THEN
B Temp ← A[i]
C A[i] ← Temp
D A[i + 1] ← Temp
E ENDIF
Line C — Line 3 should be A[i] ← A[i + 1]. A swap needs Temp to hold the first value, then the second value moves into position i, then Temp goes into position i + 1. As written, A[i] gets its own value back and the value in A[i + 1] is never moved.
Question 2
A FOR i ← 1 TO 4
B IF A[i] < A[i + 1] THEN
C swap A[i] and A[i + 1]
D ENDIF
E NEXT i
Line B — For ascending order, neighbours are in the wrong order when the first is bigger than the second, so the test must be A[i] > A[i + 1]. Using < swaps pairs that are already correct.
Question 3
A // One pass over 5 items
B FOR i ← 1 TO 5
C IF A[i] > A[i + 1] THEN
D swap A[i] and A[i + 1]
E ENDIF
F NEXT i
Line B — With 5 items there are only 4 neighbouring pairs. The loop must stop at 4, because when i is 5 the code compares A[5] with A[6], which does not exist.
Question 4
A FOR pass ← 1 TO 1
B FOR i ← 1 TO 5 - pass
C swap if A[i] > A[i + 1]
D NEXT i
E NEXT pass
Line A — One pass only moves the largest value to the end. A list of 5 items may need up to 4 passes, so the outer loop should be FOR pass ← 1 TO 4.
Question 5
A Unsorted ← 5
B FOR pass ← 1 TO 4
C Unsorted ← Unsorted + 1
D OUTPUT Unsorted
E NEXT pass
Line C — After each pass one more item is in its final place at the end, so the number of unsorted items goes down by 1. It should be Unsorted ← Unsorted - 1.

Student with a class code? Join your class →