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 list
2 Compare it with the item next to it
3 If it is bigger, swap the two items
4 Pass over: any swaps? Then pass again
5 A pass with no swaps? Sorted. Stop.

Variables

list=6, 1, 3, 2, 4pass=1swaps this pass=0

Output

 

Step 1: Five numbers, all jumbled. Pass 1 starts at the very front of the list.

1 / 16

Use Next › to go one comparison at a time, or Play to watch it run.

Step 1 of 16: Five numbers, all jumbled. Pass 1 starts at the very front of the list..

Watch out: The list is in order after pass 2, but the sort isn't finished until a pass makes no swaps.

The stopping rule

So when does it stop?

Back to the five numbers. Pass 2 has just made one swap, and the list now reads 1, 2, 3, 4, 6.

Which is closest to what you think right now?
How sure are you?

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?

Count the comparisons

How much checking is in one pass?

One pass over those eight cards. How many comparisons does it make?

Your estimate

8

016

Making bubble sort do less work

Every pair, every passvsOne pair fewer each pass

Same eight cards, same sorted result. Read down the rows and watch the right-hand side do less work.

Focus

Pass 1

Every pair, every pass

7 comparisons

One pair fewer each pass

7 comparisons

The insight

Nothing is in its final place yet, so both versions check all seven pairs.

Pass 2

Every pair, every pass

7 comparisons, including the 8 already parked at the back

One pair fewer each pass

6 comparisons

Passes 3 to 6

Every pair, every pass

7, 7, 7, 7

One pair fewer each pass

5, then 4, then 3, then 2

Whole sort of the cards

Every pair, every pass

6 passes, 42 comparisons

One pair fewer each pass

6 passes, 27 comparisons

When it stops

Every pair, every pass

After a pass with no swaps

One pair fewer each pass

After a pass with no swaps

On a big list

Every pair, every pass

Slow

One pair fewer each pass

Less work, but still slow

UK note

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

1Bubble sort compares neighbours and swaps a pair when the first item is greater.
2A pass is one full trip through the list; n items means n − 1 comparisons per pass.
3After each pass, the largest unsorted item has settled at the end.
4The sort finishes only after a pass with no swaps.
5Improvements: stop after a no-swap pass, and check one pair fewer each pass. Still one of the slowest sorts.

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?
It arranges the items in a list into a particular order, for example from lowest to highest.
How does bubble sort work, in one sentence?
It repeatedly goes through the list comparing adjacent items and swaps any pair that is in the wrong order.
What is a pass in a bubble sort?
One complete traversal of the list: each time the algorithm goes through the list is one pass.
What are adjacent items?
Items next to each other in the list: a neighbouring pair of values.
Sorting lowest to highest, when are two neighbours swapped?
When the item at the current position is greater than the item next to it.
Where is the largest item after the first pass?
At the end of the list, in its final position.
Why does the largest item reach the end in one pass?
It is greater than every item it is compared with, so it is swapped along each time.
How many comparisons does one pass over a whole list of n items make?
n − 1: one for each pair of neighbours.
When does a full bubble sort stop?
After a pass in which no swaps are made.
Why doesn't bubble sort stop on its last swap?
It can't tell that swap was the last one. A final pass with no swaps is needed to confirm the data is in order.
What are the two improvements to bubble sort's efficiency?
Stop once a pass makes no swaps, and reduce the number of comparisons by one after each pass.
Why can each pass check one pair fewer than the last?
Each pass leaves the largest unsorted item in its final place at the end, so the final pair no longer needs checking.
How efficient is bubble sort?
It is one of the slowest sorting algorithms and performs poorly in real-world use, especially on large collections of data.

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 posted

More OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

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.