GCSE · Computer Science · OCR · Spec J277

Insertion sort

Ever sorted a hand of cards? Pick up the next card, slide the bigger ones along, drop it in. That's insertion sort. Now make it precise enough for a computer.

Algorithms · Sorting

Watch the sorted part grow

Six numbers into ascending order. Step through every pass and keep your eye on the sorted sublist: one more item joins it each time.

Before you press Next: the sorted sublist is just 43. Which item will pass 1 pick up?

1 sorted sublist = the first item; unsorted sublist = all the others
2 value = a copy of the first item in the unsorted sublist
3 current = the last item in the sorted sublist
4 WHILE an item remains at current AND that item > value
5 copy that item into the next item; move current one place left
6 copy value into the item after current
7 REPEAT from line 2 until the unsorted sublist is empty

Variables

list=43, 21, 2, 80, 3, 35pass=—value=—sorted=43unsorted=21, 2, 80, 3, 35

Output

 

Step 1: Six jumbled numbers. Draw an imaginary line after the first one. One item on its own can't be out of order, so 43 is the whole sorted sublist. Everything else waits in the unsorted sublist.

1 / 20

Press Next one step at a time. Before each step, say what you think will happen to the list, then check.

Step 1 of 20: Six jumbled numbers. Draw an imaginary line after the first one. One item on its own can't be out of order, so 43 is the whole sorted sublist. Everything else waits in the unsorted sublist..

Exam line: One pass: copy the first out-of-order item into value, shift each bigger sorted item one place right, then copy value into the position after where the shifting stopped.
Watch out: A pass where nothing shifts still counts. Like the 80, the item goes straight back where it was, and the sorted sublist still grows by one.

What if you skip the copy?

Remember the 21 that vanished from the list in pass 1? Here's why that didn't matter, and when it would.

The sorted sublist is 43, 49, 64 and the next item is 51, so the list reads 43, 49, 64, 51. A classmate skips copying 51 into value and goes straight to shifting: 64 is greater than 51, so 64 is copied into the next item. What does the list hold now?

Your turn

Finish the pass

The list reads 1, 23, 45, 12. The sorted sublist is 1, 23, 45 and 12 is the first out-of-order item. Supply the missing steps of this pass.

  1. Copy the first out-of-order item into value: value = 12.Copy first, so the 12 survives the shifting.
  2. Start from the last of the ordered items: 45.
  3. missing step
Which line is step 3?
UK note

OCR J277: insertion sort is in specification section 2.1.3.

Check your thinking

Where does it start?

You're about to insertion-sort the list 7, 4, 9, 1, 6.

Before pass 1 begins, which is closest to what you think the sorted sublist holds?
How sure are you?

Insertion sort vs bubble sort

Insertion sortvsBubble sort

Two sorting algorithms, both making passes over a list. So where does the difference come from?

Focus

Passes over the list

Insertion sort

A similar number

Bubble sort

A similar number

The insight

Count the passes and you can't tell them apart. The difference is hiding inside each pass.

Comparisons in each pass

Insertion sort

Usually fewer

Bubble sort

Usually more

Overall speed

Insertion sort

Generally quicker to execute

Bubble sort

Generally slower

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Take the next item. Slide the bigger ones along. Drop it into its place.

What you need to know

  • Insertion sort works through a list, inserting each item into its correct place in an ordered sublist.
  • The list has two parts: a sorted sublist in the lower positions and an unsorted sublist in the higher ones.
  • At the start, only the first item is in the sorted sublist. Every other item is unsorted.
  • Each pass takes the first out-of-order item, compares it with the sorted items and inserts it in place.
  • Have a goYou're holding three cards: 9, 4, 7. Do just one pass of the card trick. What order are they in now?

    4, 9, 7

    One pass places one item: the 4 slides in front of the 9. The 7 hasn't been touched yet. If you said 4, 7, 9, you did the whole sort, not one pass.

  • Copy that item into value before shifting anything. Shifting overwrites its old position.
  • Shifting: while the sorted item is greater than value, copy it into the next item and move left.
  • Have a goYour mate insists that with value = 9, every item in 5, 8, 14 has to shift 'to make room'. Which ones really shift?

    Only 14.

    Only items greater than value shift. 14 is greater than 9, so it moves; 8 isn't, so the shifting stops there and 5 is never even checked.

  • When the shifting stops, copy value into the item after the current position. That pass is done.
  • Have a goMid-pass, the list reads 3, 8, 8 and value holds 5. The shifting has stopped at the 3. What does the list read after the insert?

    3, 5, 8

    value goes into the item after the current position. The current position is the 3, so 5 lands on the spare copy of 8. Writing it at the current position would wipe out the 3.

  • Each pass grows the sorted sublist by one item. Repeat until the unsorted sublist is empty.
  • Compared with bubble sort: a similar number of passes, but usually fewer comparisons per pass, so generally quicker.
  • Other algorithms do better on large, unsorted lists. On smaller or partially sorted lists, insertion sort can still be faster in practice.

The big picture

Insertion sort orders a list by growing a sorted sublist at its start, beginning with just the first item. Each pass copies the first out-of-order item into value, shifts bigger sorted items one place right, and inserts value, so the sorted sublist grows by one. Passes repeat until nothing is unsorted. It makes a similar number of passes to bubble sort but usually fewer comparisons per pass, so it's generally quicker. Other algorithms win on large, unsorted lists; insertion sort can still be faster on smaller and partially sorted ones.

Key points

1Insertion sort grows a sorted sublist at the start of the list, one item per pass, starting from just the first item.
2One pass: copy the first out-of-order item into value, shift each bigger sorted item one place right, then insert value.
3The copy comes first because shifting overwrites the item's position in the list.
4The sort ends when the unsorted sublist is empty.
5Versus bubble sort: similar passes, usually fewer comparisons per pass, generally quicker. Other algorithms beat it on large, unsorted lists.

Worked example

Problem

Use an insertion sort to put 3, 5, 8, 6, 9 into ascending order. Write down the list after each pass.

⚠ Watch out

Shifting before copying the item into value. The first shift writes over the item's position, and with no copy the item is lost for good. Copy it first, then shift.

🧠

Memory hook

Copy it, shift, insert. Forget to copy and the item vanishes.

✓

Check yourself

Without scrolling up: insertion-sort 6, 1, 5, 2. Write the list after each pass, and say which items shifted during pass 3.

Flashcards

(14)
What does an insertion sort do?
It works through a list, taking each item and inserting it into its correct place in an ordered sublist.
The two sublists in an insertion sort
A sorted sublist in the lower positions of the list and an unsorted sublist in the higher positions.
What is in the sorted sublist before pass 1?
Only the first item. Every other item is in the unsorted sublist.
Which item does each pass pick up?
The first out-of-order item, sitting at the front of the unsorted part.
What does one pass do to the sorted sublist?
It increases it by one item.
What is value used for?
It holds a copy of the item being inserted while the sorted items are shifted.
Why copy the item into value before shifting?
Shifting items right overwrites the item's position, so without a copy it would be lost.
When does a sorted item shift right?
When it is greater than value. It is copied into the next item along.
When does the shifting stop?
When the item at the current position isn't greater than value, or no items remain.
Where does value go at the end of a pass?
Into the item after the current position.
What is the stopping condition?
Passes keep repeating until no items remain unsorted. At that point every item is in its place.
An everyday insertion sort
Putting cards or books in order: make a space and slot each new one into its correct position.
Insertion sort vs bubble sort
A similar number of passes, but usually fewer comparisons per pass, so insertion sort is generally quicker.
Where can insertion sort be faster in practice, and where do other algorithms win?
Faster in practice on smaller and partially sorted lists. Other algorithms perform better on large, unsorted lists.

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.