Copyright © University of Cambridge. All rights reserved.
Take some numbered cards; between ten and twenty should be enough (you could use a suit from a pack of playing cards).
Shuffle the cards.
Then organise your deck of cards into numerical order.
What method did you use to put them in numerical order?
Can you think of any other ways you could have sorted them?
Here are some different sorting algorithms you could try. You may find it easiest to lay the cards out in a line to keep track of their order and see what's happening at a glance.
Compare the first two cards. If they are in the wrong order, swap them round. Then compare the second and third card. If they are in the wrong order, swap them round. Keep going through the pack. When you have finished, keep the cards in the new order and repeat the process from the beginning of the pack.
Repeat until you get all the way through the pack without doing any swaps. The cards are now sorted.
First pass: compare the first two cards. Swap them if necessary.
Second pass: compare the second and third cards. Swap them if necessary, and then compare the first two cards again, and swap if necessary.
Third pass: compare the third and fourth cards. Swap them if necessary, then compare second and third, swap if necessary, then compare first and second, then swap if necessary.
Continue in the same way for the rest of the pack.
Go through the pack until you find the Ace. Swap it with the first card. Then go through the pack until you find the Two. Swap it with the second card. Then go through the pack until you find the Three. Swap it with the third card. Repeat with the rest of the cards.
Put the first card down. Take the second card, and put it to the right or to the left of the first card, depending on whether it's higher or lower. Then take the third card and place it correctly relative to the first two cards, making a space if necessary. Take the fourth card and place it correctly relative to the first three cards, making a space if necessary. Keep going
until all the cards are in order in the new pile.
Take the first card of the pile. Sort the rest of the pack into numbers smaller than and numbers bigger than the number on the first card. Put the first card down between these two sub-packs. Then sort each sub-pack of cards by taking the top one and sorting the rest into two sub-packs in the same way. Keep going until there are no sub-packs with two or more cards.
Try each algorithm a few times, and keep a record of how many 'moves' or 'swaps' you do. You could work with a friend and 'race' against each other to see who sorts their pack the quickest.
If you are struggling to make sense of the written algorithms, here are some videos showing each algorithm being performed.
Here are some questions to consider:
- On average, which algorithm did you find to be quickest?
- What is the 'worst-case scenario' for each algorithm?
- How long would it take in the worst case?
- Which would you choose if you had to keep the cards in a pile rather than laying them out?
- Which would you choose if you only had a limited amount of desk space to arrange the cards on?
If you know a little about computer programming, think about how might you instruct a computer to perform these algorithms. Which ones would be quick to perform? Which ones would use a lot of space?
Notes and Background
One way to get a sense of how sorting algorithms work is to watch animations, such as the ones on this website
Thanks to Margaret for testing this problem.