Skip to main content
### Number and algebra

### Geometry and measure

### Probability and statistics

### Working mathematically

### For younger learners

### Advanced mathematics

# Sorted

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. Then compare and swap the first two cards again if necessary.

Third pass: compare the third and fourth cards. Swap them if necessary. Then compare and swap second and third cards if necessary. Then compare and swap first and second cards 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.

**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.

Or search by topic

Age 14 to 18

Challenge Level

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.

**Bubble Sort**

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.

**Shuttle Sort**

First pass: compare the first two cards. Swap them if necessary.

Second pass: compare the second and third cards. Swap them if necessary. Then compare and swap the first two cards again if necessary.

Third pass: compare the third and fourth cards. Swap them if necessary. Then compare and swap second and third cards if necessary. Then compare and swap first and second cards if necessary.

Continue in the same way for the rest of the pack.

**Selection Sort**

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.

**Insertion Sort**

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.

**Quick Sort**

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?
- If you know a little about computer programming, think about how you might instruct a computer to perform these algorithms, and how long the computer would take to perform a sort of n objects using each algorithm.

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.