SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
Edexcel 1CP2 · Topic 1 · 1.2c

Bubble
Sort

Comparing Adjacent Items · Passes · Swaps · Efficiency

CSZoneEdexcel GCSE Computer Science 1CP2
How Bubble Sort Works

Compare and Swap Adjacent Items

Bubble sort repeatedly compares adjacent pairs in a list and swaps them if they are in the wrong order. After each pass, the largest unsorted item "bubbles" to its correct position at the end. Repeat until no swaps are needed.
Pass 1 moves the largest item to the last position
Pass 2 moves the second largest to second-to-last
A list of n items requires at most n-1 passes
If no swaps occur in a pass → list is sorted → stop early
Worked Example

Sorting: [5, 3, 8, 1, 9, 2]

Pass 1: [3, 5, 1, 8, 2, 9] — 9 bubbles to end
Pass 2: [3, 1, 5, 2, 8, 9] — 8 moves to position
Pass 3: [1, 3, 2, 5, 8, 9] — 5 moves to position
Pass 4: [1, 2, 3, 5, 8, 9] — sorted!
Exam tip:Edexcel may ask you to trace bubble sort — show each pass and the list state after each one.
Efficiency Analysis

How Good is Bubble Sort?

Best case: list already sorted → only 1 pass with 0 swaps → O(n)
Worst case: list in reverse order → n-1 passes, many swaps → O(n²)
Bubble sort is simple but inefficient for large lists. Merge sort (O(n log n)) is preferred for large datasets.
Exam Practice

Have a go at this question

Edexcel-style question
Trace one complete pass of bubble sort on the list: [7, 4, 2, 9, 1]. Show the list after the pass and state how many swaps were made.
3 marks
Compare 7&4 → swap → [4,7,2,9,1]; Compare 7&2 → swap → [4,2,7,9,1]; Compare 7&9 → no swap; Compare 9&1 → swap → [4,2,7,1,9] [1]. After pass 1: [4, 2, 7, 1, 9] [1]. Total swaps = 3 [1].
Key Takeaways

What to Remember

Bubble sort compares adjacent pairs and swaps if out of order
Each pass places the next largest item in its correct position
Stop early if no swaps occur in a pass — list is already sorted
Simple but inefficient for large lists — O(n²) worst case