Contacts on a phone are listed alphabetically, high scores are ranked from best to worst, and an online shop can sort its products from cheapest to priciest in the blink of an eye. Yet a computer never "sees" the whole list at once the way our eyes do. It can only do one very small thing: compare two items, decide which is bigger, and choose whether to swap them. Repeat that small step enough times and the whole list falls into order.
Children aged roughly 7 to 12 can discover this for themselves with a stack of number cards and one simple rule. This post suggests two sorting methods to try, a fair way to count which one takes less work, and a "sorting machine" game for the whole family.
The machine's rule: compare only two cards at a time
Make 6 to 8 paper cards, each with a different number (or a number of dots, for younger children). Shuffle them and lay them face down in a row on the table. Your child plays the computer; someone else sits beside them as the "recorder" with paper and a pencil.
The game has just three rules:
- Each comparison turns over exactly two cards. Say out loud which one is smaller, then turn them back over.
- You may swap the two cards you just compared. The recorder makes a tally mark in the "compare" column for every comparison and in the "swap" column for every swap.
- No peeking from memory. The machine only knows the result of the comparison it just made, so no turning cards over to look ahead.
When finished, turn the whole row face up to check. Rule three is what makes the game like a real computer: the machine has no overall view, so it needs a clear method it can repeat until the job is done.
Method one: swap neighboring pairs
Start at the left end of the row. Compare the first card with the second: if the left one is bigger, swap them. Then compare the second card with the third, and so on to the end. After one pass, the biggest card has definitely drifted to the end of the row, like a bubble rising to the surface. Nudge it down a little to mark it as done, then make a new pass with the cards that are left.
Try it with five cards: 4, 2, 5, 1, 3.
| Pass | Comparisons | Row after the pass | Number of comparisons |
|---|---|---|---|
| 1 | 4 and 2 swap; 4 and 5 stay; 5 and 1 swap; 5 and 3 swap | 2, 4, 1, 3, 5 | 4 |
| 2 | 2 and 4 stay; 4 and 1 swap; 4 and 3 swap | 2, 1, 3, 4, 5 | 3 |
| 3 | 2 and 1 swap; 2 and 3 stay | 1, 2, 3, 4, 5 | 2 |
| 4 | 1 and 2 stay | 1, 2, 3, 4, 5 | 1 |
That makes 10 comparisons and 6 swaps in total. One small extra rule makes the machine smarter: if a whole pass needs no swaps, the row is already sorted, so stop.
Method two: find the smallest and move it to the front
This method needs a button to act as a "crown". Put the button on the first card: that is the smallest card so far. Turn over the crowned card and the next card to compare them; if the next one is smaller, move the button onto it. By the end of the row, the button sits on the smallest card. Swap that card to the front of the row, mark it as done, then look for the smallest card among the rest.
With the same five cards, 4, 2, 5, 1, 3:
- Round 1: 4 comparisons find the 1, which swaps to the front: 1, 2, 5, 4, 3.
- Round 2: 3 comparisons find the 2, already in its place, so no swap.
- Round 3: 2 comparisons find the 3, which swaps forward: 1, 2, 3, 4, 5.
- Round 4: 1 comparison shows the 4 is already in its place.
Still 10 comparisons, but only 2 swaps. Ask your child: "If swapping took a lot of effort, say the cards were two heavy boxes, which method would you pick?"
Count which method compares less
Now for the most interesting part: try both methods on a few different rows and compare the tally sheets.
| Starting row | Swap neighbors (with the early stop rule) | Find the smallest |
|---|---|---|
| 4, 2, 5, 1, 3 (jumbled) | 10 comparisons, 6 swaps | 10 comparisons, 2 swaps |
| 1, 2, 3, 5, 4 (nearly done) | 7 comparisons, 1 swap | 10 comparisons, 1 swap |
| 5, 4, 3, 2, 1 (fully reversed) | 10 comparisons, 10 swaps | 10 comparisons, 2 swaps |
Your child will see it for themselves: no method wins on every row. On a nearly sorted row, swapping neighbors can stop early; finding the smallest always makes every comparison but hardly ever swaps.
Then ask: "What happens with more cards?" With either method, the number of comparisons grows much faster than the number of cards: 5 cards need up to 10 comparisons, 8 cards up to 28, 10 cards up to 45, and 100 cards up to 4,950. That is why programmers came up with cleverer methods.
A challenge for ages 10–12: split, then merge
Split 8 cards into two halves of 4 and sort each half (splitting them further is allowed too). Now there are two sorted piles, face up, smallest on top. Compare the two top cards, move the smaller one into a new row, and repeat until both piles are empty. Merging two piles of 4 takes at most 7 comparisons. If you keep splitting right down to pairs and merge your way back up, all 8 cards need at most 17 comparisons instead of 28. With 100 cards, this method needs fewer than 600 comparisons instead of almost 5,000.
The whole family as a sorting machine
Swap the number cards for real people:
- Line up by height. Everyone stands in a jumbled line. The rule: only two people standing next to each other may compare (back to back), and the taller one moves to the right. One person is the "controller" who walks along the line calling each pair, just like method one.
- Line up by birthday. Each person writes their birth day and month on a slip of paper, folds it, and only opens it when called to compare. Try the "crown" method: whoever has the earliest birthday so far holds the button.
- Time it and count the comparisons. Next time, try the other method and compare the tally sheets. Was the faster round really the one with fewer comparisons, or had everyone just got used to the game?
After each game, ask your child: "If you had to write this method down for someone who has never played, what would you write?" Writing out clear steps like that is writing an algorithm, the part of a programmer's job that comes before typing any code.
In Dologi, the "Put in order" puzzle in the Math topic asks children to order numbers, pencils or block towers from smallest to largest, and its hint is method two from this post: "Find the smallest number and put it in box 1. Then find the smallest of the rest."
In short
- Computers sort by comparing pairs and swapping them, repeated according to a clear method; a card game with the rule "compare only two cards at a time" lets children see that.
- Two methods are easy to try at home: swap neighboring pairs, and find the smallest card and move it to the front.
- Count comparisons and swaps to compare the methods fairly; no method wins on every row.
- For older children, splitting and merging shows why long lists need a cleverer method.
Read more: Which way takes fewer steps? Teaching children to compare two ways of doing the same job and Putting things in order: from smallest to biggest, lightest to heaviest
