# Sorting algorithms

## Starter

Have a go at this sorting game. How quickly can you sort all the letters just by clicking on letters to swap?

## Introduction

### Sorting algorithms – Why do we need them?

One of the most important jobs a computer performs for us is sorting of data. Without sorted data our computer systems would be much harder to use.

Imagine:

• trying to read through all your emails if they were in a random date order.
• finding a friend’s telephone number if all you contacts were in a random order.

There are many different ordering systems we can use, common ones include:

• alphabetical order
• numerical order
• date order
• size order

Different sorting algorithms

There are many different algorithms(methods) for sorting  lists of things. In the last task some people will have sorted the letters the fastest (most time efficient) or with the least number of swap (most effort efficient). Have a chat with the person next to about how they sorted out the list. What technique did they use? Whose algorithm was the most time / effort efficient?

In computer science we have many different sorting techniques, some are most efficient than others, some are simpler than others. Today we will be looking at:

• Insertion Sort
• Bubble Sort
• Selection Sort ## Insertion Sort

### Insertion Sort

With the insertion sort algorithm, you go through each item in the list in turn and move to its correct position in the list. The algorithm is slow but simple.

Insertion Sort Tutorial

Insertion Sort Python Code

Insertion Sort Python Demo code

## Bubble Sort

### Bubble Sort

AS & A Level – You are required to know how it works and be able to write Code / Pseudocode for the algorithm

The bubble sort algorithm works by sorting through the array in pairs. It starts at the left of the array and inspects the first two items. If the items are in the wrong order they are swapped around. The algorithm then carries on with the next pair along and so forth. When it reaches the end of the array it goes back to the beginning of the array and starts again. If it completes a full pass of the array without swapping any items it knows that the array is sorted.

Bubble Sort Python Tutorial

## Activity 1

### Activity 1 – People Sorter

Replicate the class activity for each sorting algorithm (this can be done after introducing each sorting algorithm.

Best splitting the class in to groups of 15 or less (otherwise the sorting will take a rather long time!

Alternative Activity – Have a go at sorting letters using the scratch game below

## Resources

### Resources

Keywords and Definitions

Algorithm Unit Flashcards