Bubble Sort
Compare neighbouring values and swap them when they are in the wrong order.
Bubble sort repeatedly passes through the data. During each pass, neighbouring items are compared and swapped if necessary.
Predict
Read the code first. Do not run it yet.
numbers = [5, 2, 4]
if numbers[0] > numbers[1]:
temp = numbers[0]
numbers[0] = numbers[1]
numbers[1] = temp
print(numbers)- Will the first two values be swapped?
- What list is printed?
- Why is a temporary variable useful?
Investigate
Use this code to explain exactly how the program works.
numbers = [5, 2, 4, 1]
for i in range(len(numbers) - 1):
if numbers[i] > numbers[i + 1]:
temp = numbers[i]
numbers[i] = numbers[i + 1]
numbers[i + 1] = temp
print(numbers)- Trace this single pass from left to right.
- Which value finishes at the far right after the pass?
- Why are only neighbouring values compared?
Repeated passes
numbers = [5, 2, 4, 1]
for pass_number in range(len(numbers) - 1):
for i in range(len(numbers) - 1):
if numbers[i] > numbers[i + 1]:
temp = numbers[i]
numbers[i] = numbers[i + 1]
numbers[i + 1] = temp
print(numbers)[1, 2, 4, 5]
- Each pass compares adjacent pairs.
- A swap puts the larger value one position further right.
- Repeated passes eventually leave the data in order.
Self-study guide
Bubble sort repeatedly compares neighbouring values and swaps a pair when it is in the wrong order. After a complete pass in ascending order, a large value has “bubbled” towards the end. Repeated passes eventually sort the entire data set.
OCR requires understanding the main steps and code segments, applying the algorithm to a data set and identifying it. You are not required to memorise a complete implementation.
OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
Bubble sort | nested loops + adjacent comparison | Typical implementation pattern. |
compare neighbours | values[index] and values[index + 1] | Only adjacent items are compared. |
swap if wrong order | temporary variable or tuple swap | The two neighbouring positions exchange values. |
Worked example
Watch what one complete pass does to the largest value.
values = [7, 3, 9, 4, 6]
for index in range(len(values) - 1):
if values[index] > values[index + 1]:
temp = values[index]
values[index] = values[index + 1]
values[index + 1] = temp
print(values)[3, 7, 4, 6, 9]
- Compare 7 and 3: swap → 3, 7, 9, 4, 6.
- Compare 7 and 9: no swap.
- Compare 9 and 4: swap.
- Compare 9 and 6: swap.
- After one pass, 9 has reached the final position, but the list is not fully sorted yet.
Another example
A full version repeats the pass enough times.
values = [5, 2, 8, 1, 4]
for pass_number in range(len(values) - 1):
for index in range(len(values) - 1):
if values[index] > values[index + 1]:
values[index], values[index + 1] = values[index + 1], values[index]
print(values)[1, 2, 4, 5, 8]
- The outer loop repeats passes.
- The inner loop performs neighbouring comparisons.
- The exact optimisation of pass lengths is less important than understanding the repeated adjacent-swap process.
Common mistakes
- Comparing non-neighbouring values and calling it bubble sort.
- Assuming one pass always fully sorts the data.
- Using built-in
.sort()when asked to show the algorithm. - Thinking you must memorise one exact implementation for OCR.
Exam-ready checklist
- Apply one or more bubble-sort passes to a list.
- Identify adjacent comparisons and swaps in code.
- Explain what a pass achieves.
- Recognise bubble sort from a code segment.
What you have learned
- Bubble sort compares neighbouring values.
- Values are swapped when they are in the wrong order.
- Repeated passes move values towards their correct positions.