🔒
Unlock Everything
£7.99/month
or £59/year
Subscribe now →
🔢 Component 2 · 2.1 Algorithms
2.1.3a Bubble Sort
OCR J277 · GCSE Computer Science · ~12 min read
Notes
Video
Slides
Worksheet
Quiz

What Is Bubble Sort?

Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. Larger values gradually "bubble up" to the end of the list with each pass.

After each complete pass, the largest unsorted element is guaranteed to be in its correct final position.

How Bubble Sort Works

For list: [5, 3, 8, 1, 4] (sort ascending)

Pass 1 — largest value bubbles to end:
Start:
5
3
8
1
4
Compare 5,3 → swap:
3
5
8
1
4
Compare 5,8 → ok:
3
5
8
1
4
Compare 8,1 → swap:
3
5
1
8
4
Compare 8,4 → swap:
3
5
1
4
8
Pass 2:
Compare 3,5 → ok:
3
5
1
4
8
Compare 5,1 → swap:
3
1
5
4
8
Compare 5,4 → swap:
3
1
4
5
8
Pass 3:
Compare 3,1 → swap:
1
3
4
5
8
Compare 3,4 → ok:
1
3
4
5
8
Pass 4 — no swaps → SORTED:
Final:
1
3
4
5
8
🟨 Yellow = swapped this step   🟩 Green = final position

Pseudocode — Basic Bubble Sort

// Bubble sort — ascending order n = length(list) FOR pass = 1 TO n - 1 FOR i = 0 TO n - 1 - pass IF list[i] > list[i+1] THEN temp = list[i] list[i] = list[i+1] list[i+1] = temp END IF END FOR END FOR

Pseudocode — Optimised Bubble Sort (with early exit)

The optimised version adds a swap flag. If a full pass makes no swaps, the list is already sorted — exit early.

// Optimised bubble sort — exits early if already sorted n = length(list) FOR pass = 1 TO n - 1 swapped = FALSE FOR i = 0 TO n - 1 - pass IF list[i] > list[i+1] THEN temp = list[i] list[i] = list[i+1] list[i+1] = temp swapped = TRUE END IF END FOR IF swapped == FALSE THEN RETURN list // Already sorted — exit early END IF END FOR

Bubble Sort — Key Facts

PropertyValue
Number of passes (worst case)n − 1 (where n = number of items)
Comparisons per passDecreases each pass (n−pass comparisons in pass #pass)
Time complexity (worst)O(n²)
Time complexity (best — optimised)O(n) — already sorted, detected in 1 pass
Works on unsorted data?Yes — no presort needed
In-place sort?Yes — sorts within the original array

Advantages and Disadvantages

AdvantagesDisadvantages
Simple to understand and implementSlow for large lists — O(n²) worst case
Can detect already-sorted lists quickly (optimised version)Inefficient compared to merge sort for large data
Works on unsorted data — no prerequisiteMany swaps needed even for near-sorted data
In-place — no extra memory neededNot suitable for large datasets
Exam tip: In a bubble sort trace table, show every comparison and every swap. After each complete pass, the largest unsorted element moves to its correct final position — cross off the sorted elements. Count the number of swaps in each pass. For the optimised version, mention the "swap flag" — if no swaps occur in a pass, the algorithm terminates early. The inner loop runs n−1−pass times in pass number "pass".
⚠️ Common Mistakes
  • Not showing every comparison in a trace table — you must show ALL comparisons, even when no swap occurs
  • Saying the largest value goes to position 0 — it goes to the END (bubbles up to the right)
  • Forgetting the inner loop shortens by 1 each pass — after pass 1, index n-1 is in place
  • Confusing the swap: always use a temp variable — you cannot write list[i]=list[i+1]; list[i+1]=list[i]
  • Saying bubble sort is always O(n²) — the optimised version is O(n) best case for an already-sorted list
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 2.1.3a Bubble Sort

8 questions · 21 marks

Q1Describe how bubble sort works.[2]
✅ Mark scheme
Adjacent elements are compared [1] and swapped if they are in the wrong order; this is repeated until the list is fully sorted [1].
Q2Perform a bubble sort on the list [7, 2, 9, 4, 1]. Show each pass in full.[5]
✅ Mark scheme
Pass 1: [7,2,9,4,1]→swap 7,2→[2,7,9,4,1]; 7<9 no swap; swap 9,4→[2,7,4,9,1]; swap 9,1→[2,7,4,1,9] [1]. Pass 2: 2<7 no swap; swap 7,4→[2,4,7,1,9]; swap 7,1→[2,4,1,7,9] [1]. Pass 3: 2<4 no swap; swap 4,1→[2,1,4,7,9] [1]. Pass 4: swap 2,1→[1,2,4,7,9] [1]. Pass 5: no swaps, done [1].
Q3After the first pass of bubble sort on a list of n items, where is the largest value?[1]
✅ Mark scheme
At the last position (index n−1) / at the end of the list [1].
Q4What is the worst-case time complexity of bubble sort? Explain when this occurs.[2]
✅ Mark scheme
O(n²) [1] — occurs when the list is sorted in reverse order, requiring the maximum number of comparisons and swaps [1].
Q5Describe the optimisation that can be added to bubble sort to improve its best-case performance.[3]
✅ Mark scheme
Add a swap flag (Boolean variable) set to False at the start of each pass [1]. If a swap occurs, set it to True [1]. After each pass, if the flag is still False (no swaps occurred), the list is already sorted — exit early [1].
Q6Why must a temporary variable be used when swapping two elements?[2]
✅ Mark scheme
Without a temp variable, one of the values would be overwritten and lost [1]. E.g. if list[i]=list[i+1] is done first, the original value of list[i] is gone and cannot be assigned to list[i+1] [1].
Q7State two advantages and two disadvantages of bubble sort.[4]
✅ Mark scheme
Advantages (any 2): simple to understand/implement [1]; can exit early when sorted (optimised) [1]; in-place — no extra memory [1]. Disadvantages (any 2): slow for large lists — O(n²) [1]; inefficient compared to merge sort [1]; many swaps even for near-sorted data [1].
Q8For a list of 5 items, how many comparisons does the inner loop make in pass 1? In pass 2?[2]
✅ Mark scheme
Pass 1: 4 comparisons (n−1 = 5−1) [1]; Pass 2: 3 comparisons (n−2) [1]. The inner loop shortens by 1 each pass because the last element is already sorted.
?
out of 21 — self-mark above
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 10
Click to reveal definition
🎉
Complete!
TermDefinition
🎯

Mini Test — 2.1.3a Bubble Sort

10 questions · 10 marks · 10 minutes

← 2.1.2b Binary Search 2.1 Algorithms 2.1.3b Merge Sort →