Sorting Animals by Swapping
About this lesson, for parents and teachers
These animals want a class photo, shortest to tallest. The rule is tricky: you may only swap two animals standing next to each other!
Ages 6–8 · about 7 minutes · builds on Big to Small: Putting Things in Order · leads to The Sorting Race: Why Speed Matters
Try this
- Tap a round swap button to make two neighbours trade places.
- Keep swapping until the animals go from shortest to tallest.
- In the last row, you are the sorting robot. The bird points at two animals. Press Swap if the left one is taller, or Keep if not.
- Watch the end of the row lock after each pass, and see when the robot knows it is finished.
How it works
Sorting means putting things in order, like shortest to tallest or smallest number to biggest. Computers sort all the time: your songs by name, your photos by date, search results by how useful they are. To sort, you need a way to compare two things and decide which one comes first. Here the rule is simple: shorter animals go to the left, taller ones go to the right.
In this lesson you can only swap two neighbours. That sounds slow, but it always works. If you find two neighbours in the wrong order and swap them, the row gets a little closer to sorted every time. Every swap fixes one pair that was in the wrong order, so you can never get stuck. You might use a different number of swaps than a friend, and that is fine.
A computer needs exact steps, so it uses an algorithm called bubble sort. Start at the left. Compare two neighbours. If the left one is taller, swap them. Move one step right and compare again. When you reach the end, that is one pass, and the tallest animal has bubbled all the way to the end, so it is locked in place.
Then the robot does another pass, and another. Each pass locks one more animal. When the robot makes a whole pass without swapping anything, it knows every animal is in order, so it can stop early. This is why bubble sort is a real algorithm: it always finishes, and it always gets the right answer, even though it can be slow on long lists.
Where engineers use this
Spreadsheets, music apps and online shops sort lists every second. Engineers use faster sorts for big lists, but bubble sort is how many of them first learned the idea.
Words to know
- Sort
- To put things in order, like shortest to tallest.
- Swap
- Two things trade places.
- Bubble sort
- A sorting algorithm that compares neighbours and swaps them if they are in the wrong order, again and again.
- Pass
- One trip from the start of the row to the end.
Talk about it
Goal: Line animals up by height by only swapping neighbours, a real sorting algorithm.
- Why do you think the tallest animal always ends up at the end after the first pass?
- How did the robot know it was done without looking at the whole row at once?
- Can you think of something at home that is sorted, like books or socks? How was it sorted?
Without a screen: Line up 5 cups with different amounts of water, or 5 stuffed toys of different sizes. Your child may only swap two neighbours at a time. Then play robot: walk from left to right comparing each pair and swapping if needed, and count how many passes it takes.
Curriculum links: CSTA K-12 1A-AP-08 (model daily processes by creating and following algorithms); CS Unplugged sorting activities
Questions
What is bubble sort in simple words?
Bubble sort looks at two neighbours at a time and swaps them if they are in the wrong order. It walks along the list again and again until a whole trip has no swaps, which means everything is sorted.
Why is it called bubble sort?
Because big items move toward the end one swap at a time, like a bubble rising to the top of water. After each pass, the biggest unsorted item has bubbled into its final place.
Is bubble sort used in real programs?
It is mostly used for teaching and for very short lists, because it gets slow on long ones. Real programs use faster sorts like merge sort, which you can meet in later Mansori lessons.