GCSE · Computer Science · Edexcel · Spec 1CP2
Bubble sort
Look at two neighbours, swap them if they're the wrong way round, repeat. Watch the biggest item and you'll see it rise to the end like a bubble.
Algorithms · Sorting
One pass, one comparison at a time
Eight playing cards, and the ace is low, so it counts as 1. Step through a single pass into ascending order and keep your eye on the card that gets carried along.
Before you press Next: the first two cards are 6 and 4. Swap or no swap?
1 take the list of items to be sorted2 go to the next item (at the start, the first item)3 compare it with the item next to it4 IF this item > next item THEN swap the two items5 repeat until the last item in the list is reached
Variables
Output
Step 1: Eight cards in no particular order. This pass will walk along the row once, looking at just two neighbours at a time.
Press Next one comparison at a time. Before each step, say out loud whether the pair will swap.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Compare two neighbours. Swap if they're the wrong way round. Repeat until a whole pass makes no swaps.
What you need to know
- Bubble sort compares adjacent (neighbouring) items and swaps them if they're in the wrong order.
- One complete run through the list is a pass; a full pass over n items makes n − 1 comparisons.
- After each pass the largest remaining item is in its final place, but the rest of the list may still be out of order.
- The sort only stops after a whole pass that makes no swaps.
The big picture
A sorting algorithm puts the items in a list into order, for example from lowest to highest. Bubble sort does it by going through the list comparing each pair of neighbouring items and swapping any pair that's out of order. One complete run through the list is called a pass, and each pass carries the largest remaining item to the end. The sort keeps making passes until a whole pass makes no swaps. It works on words as well as numbers, but it's one of the slowest ways to sort a large amount of data.
Key points
Worked example
Problem
Use a bubble sort to put 5, 1, 4, 2 into ascending order. Write the list after each pass.
⚠ Watch out
Stopping as soon as the list looks sorted, or straight after the last swap. Bubble sort only stops after a whole pass with no swaps. Leave that final quiet pass out of your working and your answer is incomplete.
Memory hook
Biggest bubbles up; quiet pass means done. Each pass carries the heaviest hitter to the end, and only a pass with zero swaps lets you stop.
Check yourself
Without scrolling up: do one pass on 9, 4, 7, 2. How many comparisons, how many swaps, which number is now fixed, and why isn't the sort finished?
Flashcards
(15)What does a sorting algorithm do?
Bubble sort in one sentence
What is a 'pass' in bubble sort?
Swap rule when sorting into ascending order
Comparisons in one full pass over n items
Comparisons vs swaps: are they the same count?
Where is the largest item after the first pass?
Is a list always sorted after one pass?
When does bubble sort stop?
Why can't bubble sort stop on its last swap?
Two ways to make bubble sort more efficient
Why can each later pass check one fewer pair?
Can bubble sort sort words?
How does bubble sort cope with large data sets?
Factors when choosing a sorting algorithm
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 Edexcel GCSE Computer Science topics
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)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 30 September 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.