Sorting algorithms for kids: simple explanations and games

How to explain bubble sort, selection sort and insertion sort to children, with unplugged card and balance games, common mistakes and a Looponi lesson.

Updated:

The lesson “The biggest pumpkin” in Looponi: a loop compares pumpkins and remembers the record

Children sort things all the time. They line up blocks from biggest to smallest, crayons by colour, cards in their hand. They do it by eye, seeing everything at once. A computer can't. It can only compare two things at a time, and it needs to know what to do after every comparison. That exact plan is a sorting algorithm.

Sorting in plain words

Sorting means putting things in order by some rule: smallest to largest, A to Z, lightest to heaviest. Every sorting algorithm is built from the same two moves:

  1. Compare two things. Which one is smaller?
  2. Swap them or leave them. If they are in the wrong order, swap their places.

Algorithms differ in which pairs they compare and in what order. Three are easy for children to learn:

All three give the same result. They differ in how many comparisons they need, which is a good topic for older children.

Everyday examples

Unplugged sorting games

1. Bubbles on the carpet

Five children (or five soft toys) stand in a row in random order, each holding a card with a number. The rule: you may only look at your neighbour. The leader walks along the row and asks each pair: “Are you in the right order?” If not, they swap places. After the first pass, the largest number is at the end. Count the swaps on a whiteboard.

2. The balance and the mystery bottles

Prepare five identical, opaque bottles with different amounts of water or sand. Nobody may lift them by hand; they can only go on a balance scale two at a time (or on an adult's two hands, who says “this one's heavier”). The child has to line them up from lightest to heaviest. That's exactly how a computer “sees” data: only through comparing pairs. Count how many weighings it took.

3. The smallest card

Lay ten number cards face down in a row. The child may turn over two at a time and keep one face up as “the smallest so far”. After the whole row, they move the smallest card to the start of a new row. Then they look for the smallest again. That's selection sort, and good practice for working memory too.

4. Cards in your hand

Deal the child cards one by one. They slot each new card into the right place among the cards they already hold. Ask them to say it out loud: “Six is bigger than four and smaller than nine, so it goes here.” The child discovers insertion sort without needing the name.

5. A race between two methods

Two players get the same set of ten cards. One uses bubble sort, the other selection sort. A third person tallies each player's comparisons on paper. Who needs fewer? What happens if the cards start almost in order? Children notice by themselves that it depends on how the cards start.

Common mistakes

What it looks like in Looponi

In Looponi, sorting comes up in the lesson “Bubble sort” in the Lists module. The children in the story stand in a row and have to line up from shortest to tallest, each looking only at their neighbour. On the board the program reads a whole list, for example 5, 1, 4, 2, 8, and draws it as bars.

The child places the decision “are the neighbours in the wrong order?” and, under YES, a block that swaps them, then lays the pipes. Before the run they predict how many swaps 5, 1, 4, 2, 8 will need (the answer is 4). Every swap plays a note matching the height of the bar, so you can hear the sorting. Next the child predicts the swaps for a list in reverse order: 5, 4, 3, 2, 1 needs 10. The lesson also shows that 30 numbers in reverse order need 435 swaps. In the task the child changes the program so it sorts from largest to smallest, and the tests use lists of different lengths, including an empty one.

This lesson is for older children. Younger ones can start by finding the largest value in “The biggest pumpkin” at the Autumn Fair: the scales only remember the record and update it when a heavier pumpkin arrives. Finding the largest item is the first step of selection sort. With long lists, binary search becomes useful too, and it only works on a list that is already sorted.

What age is right?

Movement games with comparing neighbours and lining up by height work from age 6 or 7. Selection and insertion sort with cards, while counting comparisons, suit children of 8 or 9. Writing bubble sort as a program with two loops fits better from about 10 or 11.

Frequently asked questions

What is a sorting algorithm?

A recipe for putting things in order, for example numbers from smallest to largest. A computer can only compare two things at a time, so it needs an exact plan for what to do after each comparison.

How does bubble sort work?

Compare neighbours from the start of the list. If they are in the wrong order, swap them. After one pass the largest number is at the end. Repeat until a pass makes no swaps.

Which sorting algorithm is easiest for children?

Selection sort is a good start: find the smallest item, put it aside, repeat. Bubble sort is easy to act out, because everyone only looks at their neighbour.

Read next

All guides →