Insertion sort

You have a hand of unsorted cards. Where does the next card go, and which cards have to move to make room?

Learning goal

Predict one insertion, count the shifts and explain how cards with equal values keep their order.

Theory

Imagine sorting a hand of cards. The cards on the left are already in ascending order. The dashed outline marks the next card. Before inserting it, decide which neighbours on its left need to make room.

Listen to the explanation

Audio transcript

Think of sorting playing cards in your hand. The cards on the left are already in order. Pick up the next card. Suppose it is a five, and the sorted cards are three, seven and nine. Shift the nine right, then the seven. The five fits after the three. That took two shifts. If you meet another five, leave the older five first. This rule keeps equal cards in their original order. Look at the next card in the experiment and predict how many cards it will move.

Read full theory

A sorted start

The first card is already a sorted row of one. Pick up the next card and compare it with the sorted cards from right to left. Shift each larger card one place to the right. Put the card you picked up into the gap. The sorted part is now one card longer.

What counts as a shift?

To insert 5 into [3, 7, 9], shift 9 and 7 one place to the right. That makes two shifts. Placing the 5 into the gap is counted separately. A card already in the right place needs zero shifts.

Equal values

Only move cards with a larger value. Stop when you reach an equal value so the older card stays ahead of the newer one. A sorting algorithm that preserves this order is called stable.

1Explore

Pick out the next card and predict where it belongs. Then run one insertion to check.

2Solve it yourself

The dashed outline marks the next card. Try one pass yourself and compare the order. You can practise each exercise again with a fresh hand.

3Expert tasks

Find the fewest adjacent swaps a sorting robot can make, then audit the key comparisons across a complete sort. Harder than the tasks above – for advanced pupils and everyone who wants to get to the bottom of it.

🔒 Locked soon with an account

  • Minimum adjacent swaps
  • Audit the comparisons

How you learn here

  1. ExploreYou change the simulation yourself and watch.
  2. Predict & solveFirst you predict, then you calculate, click or build – not just type in numbers.
  3. See the consequencesAfter a mistake the simulation plays out your result and shows the gap.
  4. Write it downYou note in one sentence what you noticed – only then can you go on.
  5. New numbersAfter every mistake you get a new variant. Solving it shows you have understood – guessing does not help.

Curriculum: Secondary computer science covering sorting algorithms, lists and tracing an algorithm by hand.

For teachers: Set this lab as homework and check the proof codes of your class →

Found a mistake? Tell us – every simulation and every answer is checked by code, but people still make mistakes.