📁 Paper 1 · 3.1 Fundamentals of Algorithms
3.1.4a Sorting Algorithms — Bubble Sort
AQA 8525 · GCSE Computer Science · ~12 min read
Notes
──
Video
──
Slides
──
Worksheet
──
Quiz

What is Bubble Sort?

Bubble sort is a sorting algorithm that repeatedly compares adjacent pairs of elements and swaps them if they are in the wrong order. After each pass, the largest unsorted element "bubbles up" to its correct position at the end.

The algorithm requires multiple passes through the list, and stops when a complete pass makes no swaps (the list is sorted).

How Bubble Sort Works — Step by Step

  1. Compare element at index 0 with element at index 1
  2. If they are in the wrong order (larger first), swap them
  3. Move to the next pair (index 1 and 2), repeat
  4. Continue to the end of the unsorted portion
  5. After each pass, the largest remaining element is in its final position
  6. Repeat until no swaps occur in a full pass

Worked Example — Sort [5, 3, 8, 1, 9, 2]

Pass 1

Start:
5
3
8
1
9
2
5>3 → swap:
3
5
8
1
9
2
5<8 → ok:
3
5
8
1
9
2
8>1 → swap:
3
5
1
8
9
2
8<9 → ok:
3
5
1
8
9
2
9>2 → swap:
3
5
1
8
2
9

After pass 1: [3, 5, 1, 8, 2, 9] — 9 is in its final position ✓

Pass 2 (only compare first 5 elements)

3<5 → ok:
3
5
1
8
2
9
5>1 → swap:
3
1
5
8
2
9
5<8 → ok:
3
1
5
8
2
9
8>2 → swap:
3
1
5
2
8
9

After pass 2: [3, 1, 5, 2, 8, 9] — 8 in final position ✓

Passes continue until a full pass makes no swaps — the list is sorted.

Trace Table — Passes Summary for [5, 3, 8, 1, 9, 2]

PassList after passSwaps made?
1[3, 5, 1, 8, 2, 9]Yes (3 swaps)
2[3, 1, 5, 2, 8, 9]Yes (2 swaps)
3[1, 3, 2, 5, 8, 9]Yes (2 swaps)
4[1, 2, 3, 5, 8, 9]Yes (1 swap)
5[1, 2, 3, 5, 8, 9]No → STOP

AQA Pseudo-code

SUBROUTINE bubbleSort(list) n ← LEN(list) swappedTrue WHILE swapped = True swappedFalse FOR i0 TO n - 2 IF list[i] > list[i + 1] THEN temp ← list[i] // swap using temp variable list[i] ← list[i + 1] list[i + 1] ← temp swappedTrue ENDIF ENDFOR ENDWHILE RETURN list ENDSUBROUTINE

Key Points About Bubble Sort

PropertyDetail
Worst case comparisons~n² / 2 (approximately n²)
n = 1,000 items~1,000,000 comparisons in worst case
Works on unsorted lists?Yes — works on any list
Early terminationStops as soon as no swaps in one pass (optimisation)
Swap mechanismRequires a temp variable to swap without data loss
Exam tip: Always use a temp variable when describing or writing a swap — you cannot just write list[i] ← list[i+1] directly. The optimisation of stopping when no swaps occur is important — mention it in exam answers.
⚠️ Common Mistakes
  • Swapping without a temp variable — this overwrites data. Always use temp ← list[i]; list[i] ← list[i+1]; list[i+1] ← temp
  • Forgetting the optimisation: bubble sort can stop early if no swaps were made in a pass
  • Getting the FOR loop bound wrong: loop runs from 0 TO n-2 (not n-1) to avoid going out of bounds
  • Confusing which element "bubbles up" — the largest element moves to the end first
Video coming soon
This lesson video is in production

Key points covered in this video

  • How adjacent pairs are compared and swapped
  • Visual walkthrough: sorting [5, 3, 8, 1, 9, 2] pass by pass
  • The role of the temp variable in swapping
  • Early termination optimisation
  • AQA pseudo-code for bubble sort
Click slide or press arrow keys to navigate
✍️

Exam-style Worksheet — 3.1.4a Bubble Sort

8 AQA-style questions · 23 marks · Mark schemes revealed on submit

Q1Describe how bubble sort works.[3 marks]
✅ Mark scheme
Mark scheme
Repeatedly compares adjacent pairs [1]; swaps them if in the wrong order [1]; repeats until no swaps occur in a complete pass [1].
Q2Perform one complete pass of bubble sort on [4, 2, 7, 1, 5]. Show the list after each swap.[4 marks]
✅ Mark scheme
Mark scheme
4>2 swap → [2,4,7,1,5] [1]; 4<7 no swap [1]; 7>1 swap → [2,4,1,7,5] [1]; 7>5 swap → [2,4,1,5,7] — 7 in final position [1].
Q3Why is a temp variable needed when swapping two elements? Show the three lines of pseudo-code needed.[3 marks]
✅ Mark scheme
Mark scheme
Without temp, the original value of list[i] would be overwritten/lost before it can be placed in list[i+1] [1]; temp ← list[i] [1]; list[i] ← list[i+1]; list[i+1] ← temp [1].
Q4Describe the optimisation that can make bubble sort more efficient in practice.[2 marks]
✅ Mark scheme
Mark scheme
If no swaps are made during a complete pass [1], the algorithm stops early / terminates because the list is already sorted [1].
Q5After pass 1 of bubble sort on [6, 3, 9, 2, 7], what is guaranteed to be in its correct final position?[1 mark]
✅ Mark scheme
Mark scheme
The largest element (9) will be in its correct final position at the end of the list [1].
Q6Write the AQA pseudo-code for bubble sort. Include: WHILE loop, FOR loop, swap using temp, and the optimisation.[5 marks]
✅ Mark scheme
Mark scheme
swapped ← True; WHILE swapped = True [1]; swapped ← False inside WHILE [1]; FOR i ← 0 TO n-2 [1]; IF list[i] > list[i+1] THEN ... swap using temp, swapped ← True [1]; ENDFOR / ENDWHILE [1].
Q7A list of 1,000 items is sorted using bubble sort. Estimate the worst-case number of comparisons.[2 marks]
✅ Mark scheme
Mark scheme
~1,000,000 comparisons [1]; because bubble sort is O(n²) and 1,000² = 1,000,000 [1].
Q8Give one advantage and one disadvantage of bubble sort compared to merge sort.[3 marks]
✅ Mark scheme
Mark scheme
Advantage: simpler to understand and implement [1]; has early termination optimisation for nearly-sorted lists [1 alt]. Disadvantage: O(n²) — much slower than merge sort O(n log n) for large lists [1].
Compare your answers to the mark schemes above.
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 6
Click to flip
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — 3.1.4a Bubble Sort

Timed exam conditions. No feedback until you submit.

  • 8 questions · 8 marks · 10 minutes
  • 5 multiple choice + 3 short answer
  • Mark schemes revealed after submission
← 3.1.3b Binary Search
7 of 57 · AQA 8525
3.1.4b Merge Sort →