KS3 · Computer Science
Comparing sorting algorithms
Two friends sort the same cards, 8, 3, 5, 1, into 1, 3, 5, 8. One makes 12 comparisons, the other 6. Equally good?
Computer Science · Algorithms
Bubble sort, one comparison at a time
Step through bubble sort on [8, 3, 5, 1]. Keep an eye on the tally: every look at a pair counts, swap or no swap.
Before you press Next: which number will be sitting at the right-hand end when pass 1 finishes?
1 REPEAT (each time round is one pass)2 FOR each neighbouring pair, left to right3 IF left item > right item THEN4 swap the two items5 UNTIL a whole pass makes no swaps6 OUTPUT the list
Variables
Output
Step 1: Here's our jumbled list. Pass 1 starts at the left-hand end. Make your prediction, then press Next.
Press Next one step at a time and say the next comparison out loud before you reveal it.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Two algorithms, one job: put a list in order. They land on the same answer. The interesting bit is how much work each one does to get there.
What you need to know
- A sorting algorithm puts a list in order: ascending or descending, numerical or alphabetical.
- Bubble sort compares each pair of neighbouring items and swaps them if they're the wrong way round.
- One trip along the list is a pass. After it, the largest remaining item has bubbled to the end.
Have a goBubble sort makes pass 1 on [6, 2, 9, 4]. Which number is guaranteed to be at the right-hand end afterwards?
9
4 started at the end, but 9 is bigger, so they swap. The largest item always gets carried to the end of a pass.
- Bubble sort keeps making passes until a whole pass makes no swaps. That's its proof the list is sorted.
Have a goYour mate Zak watches a pass finish with [1, 2, 4, 9], punches the air and yells 'Done!' That pass made one swap. Does bubble sort stop?
Not yet. It makes one more full pass.
Looking sorted isn't enough. A swap means no proof yet; bubble sort only stops after a whole pass with no swaps. Sorry, Zak.
- Insertion sort treats the first item as the sorted part, then inserts each next item into its correct place there.
- To make room for the item being inserted, larger items in the sorted part move along one place.
Have a goInsertion sort has reached [2, 5, 8 | 6, 1]. Where does 6 end up?
Between 5 and 8: [2, 5, 6, 8 | 1]
8 is bigger, so it moves along; 5 is smaller, so 6 stops there. It doesn't just go to the front or the end.
- Both reach the same sorted list by different steps, so compare the work: comparisons and swaps or moves.
- On the same list, insertion sort usually needs fewer comparisons and moves, so it's usually quicker. Usually, not always.
- Already or nearly in order? Insertion sort does very little work, and bubble sort can stop early.
- On long, jumbled lists both get slow, because the work grows much faster than the length of the list.
The big picture
Bubble sort and insertion sort solve the same problem: putting a list in order. They get the same sorted list, but by different steps, so you compare them by the work they do (comparisons and swaps or moves), how quickly they finish and how simple they are. Insertion sort usually needs fewer comparisons and moves, so it's usually quicker. On a list that's already or nearly in order it does very little work, and bubble sort can stop early too. On long, jumbled lists both get slow. To choose, look at the list's length and how close it already is to being in order.
Key points
Worked example
Problem
A teacher's five quiz scores are nearly in order: [2, 4, 3, 7, 9]. Sort them into ascending order with bubble sort, keeping a tally of comparisons and swaps. Then work out whether insertion sort would do more or less work on this list.
⚠ Watch out
Thinking the same sorted list means the same work. On [8, 3, 5, 1] bubble sort made 12 comparisons, insertion sort 6. The opposite slip: one trace where insertion sort wins doesn't prove it always wins.
Memory hook
Bubbles rise: in bubble sort the biggest item floats to the end, one pass at a time. Insertion is sorting a hand of cards: pick up the next card and slide it into the right spot.
Check yourself
Cover the page. Trace both algorithms on [4, 1, 3, 2], counting comparisons. Why does bubble sort's last pass make no swaps? When would both do the same work?
Flashcards
(15)What does a sorting algorithm do?
In bubble sort, which items get compared?
What is a 'pass' in bubble sort?
Where is the largest remaining item after one pass of bubble sort?
When does bubble sort stop?
In insertion sort, what counts as the sorted part at the very start?
In insertion sort, what happens to the next item from the unsorted part?
When does insertion sort stop?
What does it mean to trace a sorting algorithm?
Two algorithms give the same sorted list. How do you compare them?
Does insertion sort always do less work than bubble sort?
What happens on a list that's already or nearly in order?
Why do both algorithms get slow on long, jumbled lists?
What does 'sorting in place' mean?
Which two things about a list help you choose between bubble sort and insertion 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 KS3 Computer Science topics
How this lesson was checked. This KS3 Computer Sciencelesson 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 27 October 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.