GCSE · Computer Science · OCR · Spec J277
Bubble sort
Bubble sort can only ever look at two neighbours at a time. No overview, no shortcuts. And yet, pass by pass, the list sorts itself from the back. Here's how.
Computer Science · Algorithms
Watch a bubble sort, one pair at a time
Five jumbled numbers, sorted smallest to largest. Step through and track the 6.
Press Next and follow the 6. Then ask: when is the sort really finished?
1 Start at the first item in the list2 Compare it with the item next to it3 If it is bigger, swap the two items4 Pass over: any swaps? Then pass again5 A pass with no swaps? Sorted. Stop.
Variables
Output
Step 1: Five numbers, all jumbled. Pass 1 starts at the very front of the list.
Use Next › to go one comparison at a time, or Play to watch it run.
Predict, then check
New list: eight playing cards, where A is an ace and counts as 1. Think about what happened to the 6 in the trace.
Pass 1 on the cards 6, 4, 5, 2, 8, A, 7, 3. Where is the 8 when the pass ends?
OCR J277 specification reference: section 2.1.3.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
What you need to know
- A sorting algorithm arranges the items in a list into a particular order, such as lowest to highest.
- Bubble sort repeatedly goes through the list, comparing adjacent items (neighbouring pairs) and swapping any pair in the wrong order.
- Each complete trip through the list is called a pass.
- In a pass, start at the first item, compare it with its neighbour, swap if it's greater, then move along until the last item.
Have a goSorting smallest to largest. Which of these neighbour pairs gets swapped: (3, 7), (8, 5) or (4, 4)?
Only (8, 5).
(4, 4) is the tempting one, but a swap needs the left item to be greater than its neighbour, and equal isn't greater. (3, 7) is already the right way round.
- One pass carries the largest item to the end, into its final place: it's greater than every item it meets.
- Every pass over the whole list makes one comparison per pair: the number of items minus one.
Have a goA pass over a whole list made 11 comparisons. How many items are in the list?
12 items.
Comparisons are one per neighbouring pair, which is one fewer than the number of items. Saying 11 mixes up the pairs with the items.
- Keep passing until a pass makes no swaps. The sort doesn't stop on the last swap: that final pass confirms the order.
Have a goMax hands you a list that is already sorted, 1, 2, 3, 4, and grins: “Bet your bubble sort does zero passes.” How many passes does it actually make?
One pass.
Zero is tempting, but the sort can only stop after a pass with no swaps. So it makes one full pass, finds nothing to swap, and that pass confirms the order.
- Two improvements: stop once a pass makes no swaps, and make one fewer comparison after each pass.
- Speed-wise, bubble sort is one of the slowest sorting algorithms and performs poorly in real-world use, especially on large data sets.
The big picture
Bubble sort puts a list in order by comparing neighbouring items and swapping any pair in the wrong order, one pass after another. Each pass carries the largest unsorted item to the end, a pass over n items makes n − 1 comparisons, and the sort stops only after a pass with no swaps. Two tweaks make it more efficient, but it's still one of the slowest sorting algorithms.
Key points
Worked example
Problem
Carry out the first pass of a bubble sort, smallest to largest, on 43, 21, 2, 50, 3, 80, 35, 64, 7. Give the list after the pass and count the comparisons and swaps.
⚠ Watch out
Stopping as soon as the list looks sorted, or straight after the last swap. Bubble sort needs one more complete pass with no swaps to confirm the order. A second common slip is counting comparisons as the number of items: one pass over 9 items makes 8 comparisons, not 9.
Memory hook
Biggest bubbles to the back. Pairs, not items: n − 1. No swaps? Now stop.
Check yourself
Without looking back, explain: why the largest item ends pass 1 at the back, why 6 items mean 5 comparisons per pass, and why the sort needs a swap-free pass to stop.
Flashcards
(13)What does a sorting algorithm do?
How does bubble sort work, in one sentence?
What is a pass in a bubble sort?
What are adjacent items?
Sorting lowest to highest, when are two neighbours swapped?
Where is the largest item after the first pass?
Why does the largest item reach the end in one pass?
How many comparisons does one pass over a whole list of n items make?
When does a full bubble sort stop?
Why doesn't bubble sort stop on its last swap?
What are the two improvements to bubble sort's efficiency?
Why can each pass check one pair fewer than the last?
How efficient is bubble sort?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore OCR GCSE Computer Science topics
How this lesson was checked. This OCR GCSE Computer Science (specification J277)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 7 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.