You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers the bubble sort algorithm as required by OCR J277 Section 2.2. Bubble sort is one of the three standard sorting algorithms you must know for the OCR GCSE Computer Science exam. You need to understand how it works, be able to write it in pseudocode, trace through it, and evaluate its efficiency.
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent (neighbouring) elements, and swaps them if they are in the wrong order. This process is repeated until no more swaps are needed, meaning the list is sorted.
The algorithm gets its name because smaller values gradually "bubble" to the top (front) of the list while larger values sink to the bottom (end), like bubbles rising in water.
The following flowchart shows the bubble sort comparison and swap process:
flowchart TD
A([Start]) --> B["Set swapped = true"]
B --> C{swapped = true?}
C -- No --> Z(["List is sorted\nEnd"])
C -- Yes --> D["Set swapped = false\nStart at first pair"]
D --> E{"Current >\nNext?"}
E -- Yes --> F["Swap the\ntwo elements"]
F --> G["Set swapped = true"]
G --> H{More pairs?}
E -- No --> H
H -- Yes --> I["Move to next pair"]
I --> E
H -- No --> C
procedure bubbleSort(list)
n = list.length
swapped = true
while swapped == true
swapped = false
for i = 0 to n - 2
if list[i] > list[i + 1] then
temp = list[i]
list[i] = list[i + 1]
list[i + 1] = temp
swapped = true
endif
next i
n = n - 1
endwhile
endprocedure
def bubble_sort(lst):
n = len(lst)
swapped = True
while swapped:
swapped = False
for i in range(n - 1):
if lst[i] > lst[i + 1]:
lst[i], lst[i + 1] = lst[i + 1], lst[i]
swapped = True
n -= 1
return lst
numbers = [5, 3, 8, 1, 2]
print(bubble_sort(numbers)) # Output: [1, 2, 3, 5, 8]
OCR Exam Tip: The optimised version reduces
nby 1 after each pass because the last element is guaranteed to be in the correct position. Also, using aswappedflag to detect early completion is an important optimisation — mention it if asked about improving bubble sort.
Let us sort the list [5, 3, 8, 1, 2] using bubble sort:
Pass 1:
| Comparison | List State | Swap? |
|---|---|---|
| 5 > 3? Yes | [3, 5, 8, 1, 2] | Yes |
| 5 > 8? No | [3, 5, 8, 1, 2] | No |
| 8 > 1? Yes | [3, 5, 1, 8, 2] | Yes |
| 8 > 2? Yes | [3, 5, 1, 2, 8] | Yes |
After Pass 1: [3, 5, 1, 2, 8] — 8 is in its correct position.
Pass 2:
| Comparison | List State | Swap? |
|---|---|---|
| 3 > 5? No | [3, 5, 1, 2, 8] | No |
| 5 > 1? Yes | [3, 1, 5, 2, 8] | Yes |
| 5 > 2? Yes | [3, 1, 2, 5, 8] | Yes |
After Pass 2: [3, 1, 2, 5, 8] — 5 is in its correct position.
Pass 3:
| Comparison | List State | Swap? |
|---|---|---|
| 3 > 1? Yes | [1, 3, 2, 5, 8] | Yes |
| 3 > 2? Yes | [1, 2, 3, 5, 8] | Yes |
After Pass 3: [1, 2, 3, 5, 8] — 3 is in its correct position.
Pass 4: No swaps needed — list is sorted.
| Property | Detail |
|---|---|
| Best case | O(n) — already sorted, one pass with no swaps |
| Worst case | O(n²) — data is in reverse order |
| Average case | O(n²) |
| Space complexity | O(1) — sorts in place, no extra list needed |
| Stability | Stable — equal elements maintain their relative order |
| Adaptive | Yes — detects if already sorted (with swapped flag) |
OCR Exam Tip: When asked to "show the state of the list after each pass", you must show the complete list after every full pass through the data — not after every individual swap. This is a common mistake that loses marks.
Let us sort [5, 4, 3, 2, 1] with bubble sort. This is the worst-case scenario for bubble sort because every pair must be swapped.
Pass 1:
| Comparison | List State | Swap? |
|---|---|---|
| 5 > 4? Yes | [4, 5, 3, 2, 1] | Yes |
| 5 > 3? Yes | [4, 3, 5, 2, 1] | Yes |
| 5 > 2? Yes | [4, 3, 2, 5, 1] | Yes |
| 5 > 1? Yes | [4, 3, 2, 1, 5] | Yes |
After Pass 1: [4, 3, 2, 1, 5].
Pass 2:
| Comparison | List State | Swap? |
|---|---|---|
| 4 > 3? Yes | [3, 4, 2, 1, 5] | Yes |
| 4 > 2? Yes | [3, 2, 4, 1, 5] | Yes |
| 4 > 1? Yes | [3, 2, 1, 4, 5] | Yes |
After Pass 2: [3, 2, 1, 4, 5].
Pass 3: After swaps, list becomes [2, 1, 3, 4, 5].
Pass 4: After swaps, list becomes [1, 2, 3, 4, 5].
Pass 5: No swaps — sorted.
For n = 5 elements in reverse order, bubble sort needed 4 + 3 + 2 + 1 = 10 swaps and 5 full passes. This illustrates the O(n²) worst case.
Now let us sort the already-sorted list [1, 2, 3, 4, 5]:
Pass 1:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.