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 right
3 IF left item > right item THEN
4 swap the two items
5 UNTIL a whole pass makes no swaps
6 OUTPUT the list

Variables

list=[8, 3, 5, 1]pass=1swaps=0comparisons=0swaps this pass=0

Output

 

Step 1: Here's our jumbled list. Pass 1 starts at the left-hand end. Make your prediction, then press Next.

1 / 18

Press Next one step at a time and say the next comparison out loud before you reveal it.

Step 1 of 18: Here's our jumbled list. Pass 1 starts at the left-hand end. Make your prediction, then press Next..

Exam line: Bubble sort stops only after a whole pass with no swaps. On [8, 3, 5, 1]: 4 passes, 12 comparisons, 5 swaps.
Watch out: The list looks sorted at the end of pass 3. Bubble sort still isn't allowed to stop. Find out why.

Your turn · Insertion sort

Same list, insertion sort

Insertion sort the same list bubble sort just sorted: [8, 3, 5, 1]. The bar | splits the sorted part (left) from the unsorted part (right). We count each comparison with a sorted item, and each time a sorted item moves along one place. Pick each missing step.

  1. Start: [8 | 3, 5, 1]. The first item, 8, is the sorted part all on its own.Tally: 0 comparisons, 0 moves.
  2. Take 3. Compare with 8: 8 is bigger, so 8 moves one place right. Nothing left to compare, so 3 goes at the front → [3, 8 | 5, 1].Tally: 1 comparison, 1 move.
  3. missing step
Which line is step 3?

Exam line: Insertion sort finished [8, 3, 5, 1] with 6 comparisons and 5 moves. Same sorted list as bubble sort, different amount of work.

Bubble sort vs insertion sort

Bubble sortvsInsertion sort

Start at the top row. It's the one people get wrong most often.

Focus

The final result

Bubble sort

[8, 3, 5, 1] becomes [1, 3, 5, 8]

Insertion sort

[8, 3, 5, 1] becomes [1, 3, 5, 8]

The insight

Identical. So 'which is better?' is never about the answer. It's about the work each one does to get there.

How it works

Bubble sort

Compares neighbouring pairs and swaps any in the wrong order, pass after pass

Insertion sort

Takes the next unsorted item and slides it into its place in the sorted part

Work on [8, 3, 5, 1]

Bubble sort

12 comparisons, 5 swaps

Insertion sort

6 comparisons, 5 moves

Already in order: [1, 3, 5, 8]

Bubble sort

One pass, 3 comparisons, no swaps, then it stops early

Insertion sort

3 comparisons, no moves: every item is already in its place

Nearly in order, but the small item is at the end: [2, 3, 4, 7, 1]

Bubble sort

5 passes: 20 comparisons, 4 swaps

Insertion sort

7 comparisons, 4 moves

Long, jumbled lists (back-to-front order)

Bubble sort

4 items: 12 comparisons. 8 items: 56

Insertion sort

4 items: 6 comparisons. 8 items: 28

Your call

Is insertion sort always the better choice?

The claim

Insertion sort is always the better choice than bubble sort.

Place each piece of evidence to load the balance. Mark the strong ones — they count double.

  1. On [8, 3, 5, 1], insertion sort made 6 comparisons; bubble sort made 12.

    Evidence 1: does it support or challenge the claim?
  2. On [2, 3, 4, 7, 1], insertion sort made 7 comparisons; bubble sort needed 5 passes and 20.

    Evidence 2: does it support or challenge the claim?
  3. On [1, 3, 5, 8], already in order, both make 3 comparisons and bubble sort stops after one pass.

    Evidence 3: does it support or challenge the claim?
  4. Both algorithms give exactly the same sorted list, and both are simple to understand and write.

    Evidence 4: does it support or challenge the claim?
  5. On long, jumbled lists both become slow, because the work grows much faster than the length.

    Evidence 5: does it support or challenge the claim?
  6. Insertion sort only usually needs fewer comparisons and moves; a few traces can't prove 'always'.

    Evidence 6: does it support or challenge the claim?

Exam line: A good judgement names the list: how long it is and how close it already is to being in order.

Check your thinking

What do you really think?

Bubble sort and insertion sort have both sorted [8, 3, 5, 1] into [1, 3, 5, 8]. Along the way, bubble sort's pass 3 left the list looking sorted, but that pass made a swap.

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

Exam line: Same result, different work. And bubble sort needs a whole pass with no swaps before it stops.

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

1Bubble sort swaps neighbouring pairs, pass after pass, and stops after a whole pass with no swaps.
2Insertion sort grows a sorted part, inserting each next item into its place and moving larger items along.
3Same sorted result, different work: compare comparisons and swaps or moves, speed and simplicity.
4Insertion sort is usually quicker; on a list already in order both do very little, and on long jumbled lists both get slow.
5To choose, look at the list's length and how close it already is to being in order.

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?
Puts the items of a list into order, such as ascending or descending, numerical or alphabetical.
In bubble sort, which items get compared?
Each pair of neighbouring (adjacent) items. If a pair is the wrong way round, they're swapped.
What is a 'pass' in bubble sort?
One trip along the list, comparing each neighbouring pair in turn.
Where is the largest remaining item after one pass of bubble sort?
At the end of the unsorted part. It has 'bubbled' there.
When does bubble sort stop?
After a whole pass that makes no swaps. That pass proves the list is sorted.
In insertion sort, what counts as the sorted part at the very start?
Just the first item, on its own.
In insertion sort, what happens to the next item from the unsorted part?
It's inserted into its correct place in the sorted part, with larger items moving along one place to make room.
When does insertion sort stop?
When every item has been inserted, so the whole list is sorted.
What does it mean to trace a sorting algorithm?
Carry it out step by step, writing down the state of the list after each pass or each insertion.
Two algorithms give the same sorted list. How do you compare them?
By the work each does (how many comparisons and swaps or moves), how quickly it finishes and how simple it is.
Does insertion sort always do less work than bubble sort?
No. It usually needs fewer comparisons and moves. On a list already in order, both make the same few comparisons.
What happens on a list that's already or nearly in order?
Insertion sort does very little work. Bubble sort can stop early, once a pass makes no swaps.
Why do both algorithms get slow on long, jumbled lists?
The amount of work grows much faster than the length of the list.
What does 'sorting in place' mean?
Rearranging the items inside the list itself, rather than building a new list. Both of these algorithms do it.
Which two things about a list help you choose between bubble sort and insertion sort?
How long the list is, and how close it already is to being in order.

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 KS3 Computer Science topics

See the full KS3 Computer Science curriculum →

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.

Helpful guides for parents and students