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 IFEND FOREND 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 = FALSEFOR 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 = TRUEEND IFEND FORIF swapped == FALSETHENRETURN list // Already sorted — exit earlyEND IFEND FOR
Bubble Sort — Key Facts
Property
Value
Number of passes (worst case)
n − 1 (where n = number of items)
Comparisons per pass
Decreases 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
Advantages
Disadvantages
Simple to understand and implement
Slow 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 prerequisite
Many swaps needed even for near-sorted data
In-place — no extra memory needed
Not 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]
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!
Term
Definition
🎯
Mini Test — 2.1.3a Bubble Sort
10 questions · 10 marks · 10 minutes
⏱ 10:00
10 marks
Section A — Multiple Choice [5 marks]
Q1What does bubble sort compare during each pass?
Q2After pass 1 of bubble sort on [6,3,8,2], what does the list look like?
Q3What is the worst-case time complexity of bubble sort?
Q4What variable is used in the optimised bubble sort to allow early exit?
Q5In bubble sort, why is a temp variable needed for a swap?
Section B — Short Answer [5 marks]
Q6Describe how bubble sort sorts a list in ascending order.
Mark schemeRepeatedly compare adjacent elements [1]; swap them if the left one is greater [1]; repeat passes until no swaps occur [1]. Award 1-2 marks for clear description.
Q7After pass 1 of bubble sort, where is the largest element? Explain why.
Mark schemeAt the end/last index [1] — the largest element is always larger than any adjacent element, so it is swapped all the way to the end during pass 1 [1].
Q8Explain what "in-place sorting" means and give an example of whether bubble sort is in-place.
Mark schemeIn-place sorting means the algorithm sorts within the original data structure without creating a new copy [1]. Bubble sort IS in-place — it only uses a single temp variable for swaps [1].
Q9For [4, 1, 7, 3, 9], how many comparisons are made in pass 1?
Mark scheme4 comparisons [1] — the inner loop runs n−1 times in pass 1 (where n=5, so 5−1=4). Pairs compared: (4,1), (4,7), (7,3), (7,9). Note: after swap (4,1)→(1,4), then compare 4 and 7 etc. [1]
Q10State one advantage and one disadvantage of bubble sort compared to merge sort.
Mark schemeAdvantage: bubble sort is simpler to understand and implement [1]. Disadvantage: bubble sort is much slower — O(n²) vs O(n log n) for merge sort, making it unsuitable for large datasets [1].