# Sorted

## Problem

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.

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

## Getting Started

Bubble Sort

Shuttle Sort

Selection Sort

Insertion Sort

Quick Sort

## Teachers' Resources

### Why do this problem?

This problem uses the context of sorting playing cards as a hook to engage students' curiosity about efficiency of different sorting algorithms.The task could be used to introduce the idea of algorithmic complexity, by looking at how long each method takes and considering worst-case scenarios. It could also be used as a programming exercise.

By presenting many different algorithms that perform the same function, we hope students will reflect on the benefits of having a variety of tools in their mathematical toolkit when solving problems.

### Possible approach

Each student will need one suit from a pack of cards."Shuffle your cards, and then put them in order from Ace to King. Watch how your partner puts their cards in order. Do you both do it the same way? How efficient is your method?"

**Either**show students each video and invite them to make sense of the algorithm and have a go themselves,

**or**hand out this worksheet for them to make sense of the algorithms on paper.

"For each of the algorithms, perform it a few times to get a feel for it. Then choose two algorithms and compare them. Which is the quickest? Why? Can you put your cards into a worst-case scenario for each of the algorithms, to make it take as long as possible?"

Give students time to explore these, together with the questions from the problem which are on the worksheet:

- 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?