23-bubble-sort.py
python-programming / searching-and-sorting  ·  Lesson 24
Lesson 24 — Searching and sorting

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.

bubble-sort-predict.py
numbers = [5, 2, 4]
if numbers[0] > numbers[1]:
    temp = numbers[0]
    numbers[0] = numbers[1]
    numbers[1] = temp
print(numbers)
Predict before running
  • 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.

bubble-sort-investigate.py
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)
Investigate the code
  • 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

bubble_sort.py
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)
Output
[1, 2, 4, 5]
How it works
  • Each pass compares adjacent pairs.
  • A swap puts the larger value one position further right.
  • Repeated passes eventually leave the data in order.
Understand the behaviourYou should be able to follow the comparisons and swaps on a data set; the exact loop bounds can vary between implementations.
Recognise and apply the algorithmBe able to follow these characteristic steps on a data set and recognise the algorithm from program code. Correct implementations can use different variable names or loop structures, so focus on the behaviour rather than memorising one exact version.

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 focusRecognise adjacent comparison + swap + repeated passes as bubble sort. Be able to carry out passes on paper and interpret code segments.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
Bubble sortnested loops + adjacent comparisonTypical implementation pattern.
compare neighboursvalues[index] and values[index + 1]Only adjacent items are compared.
swap if wrong ordertemporary variable or tuple swapThe two neighbouring positions exchange values.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

Watch what one complete pass does to the largest value.

lesson-24-worked.py
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)
Output
[3, 7, 4, 6, 9]
Work through it line by line
  • 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.

lesson-24-example-2.py
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)
Output
[1, 2, 4, 5, 8]
Why this example matters
  • 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

Avoid these errors
  • 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-safe warningOCR expects algorithm understanding and application, not rote memorisation of the complete code.

Exam-ready checklist

You should now be able to
  • 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.