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 others2 value = a copy of the first item in the unsorted sublist3 current = the last item in the sorted sublist4 WHILE an item remains at current AND that item > value5 copy that item into the next item; move current one place left6 copy value into the item after current7 REPEAT from line 2 until the unsorted sublist is empty
Variables
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.
Press Next one step at a time. Before each step, say what you think will happen to the list, then check.
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?
OCR J277: insertion sort is in specification section 2.1.3.
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
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?
The two sublists in an insertion sort
What is in the sorted sublist before pass 1?
Which item does each pass pick up?
What does one pass do to the sorted sublist?
What is value used for?
Why copy the item into value before shifting?
When does a sorted item shift right?
When does the shifting stop?
Where does value go at the end of a pass?
What is the stopping condition?
An everyday insertion sort
Insertion sort vs bubble sort
Where can insertion sort be faster in practice, and where do other algorithms win?
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 OCR GCSE Computer Science topics
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.