You are on page 1of 6

2

http://www.furthermaths.org.uk

Sorting Algorithms

Wiltshire

Decision Maths
Sorting Algorithms
(Algoritma Mengisih)

Something as simple as sorting a list of numbers needs an algorithm on a computer. Look at the list of numbers below; 8, 3, 5, 2, 7, 9, 1, 4, 6 Clearly it is easy to see how the numbers would go from smallest to biggest. However a computer, does not have the ability to do this as simply as we can. It would use a sorting algorithm; we are going to look at the following algorithms to sort these numbers:
Bubble Sort Algorithm Selection with interchange (tukarganti) Sort Algorithm Quick (pantas) Sort Algorithm Shuttle Sort Algorithm

Bubble Sort Algorithm


1st Iteration
8 3 3 3 3 3 3 3 3 3 8 5 5 5 5 5 5 5 5 5 8 2 2 2 2 2 2 2 2 2 8 7 7 7 7 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 9 1 1 1 1 1 1 1 1 1 9 4 4 4 4 4 4 4 4 4 9 6 6 6 6 6 6 6 6 6 9

Wiltshire

Bubble Sort Algorithm


2nd Iteration
3 3 3 3 3 3 3 3 3 5 5 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 7 8 8 8 8 8 1 1 1 1 1 1 1 1 1 8 4 4 4 4 4 4 4 4 4 8 6 6 6 6 6 6 6 6 6 8 8 9 9 9 9 9 9 9 9 9

Wiltshire

We are going to sort out this list in to ascending order. The first thing to do is to compare the first two numbers to see if they are the correct way around. In this case they are not, so we change them. Now repeat the process on the next pair of numbers, and so on. The process (or iteration) stops when all the pairs have been compared. The final line shows the State of the order after the first iteration.

The process now begins again starting with the new state of the line. It is important to show all of your working and do not skip or assume any steps. Remember to show the state of the line at the end of each iteration.

8 comparisons, 7 swaps

7 comparisons, 4 swaps

Bubble Sort Algorithm


3rd Iteration
3 2 2 2 2 2 2 2 3 3 3 3 3 3 5 5 5 5 5 5 5 7 7 7 7 1 1 1 1 1 1 1 7 4 4 4 4 4 4 4 7 6 6 6 6 6 6 6 7 8 8 8 8 8 8 8 9 9 9 9 9 9 9

Wiltshire

Bubble Sort Algorithm


4th Iteration
2 2 2 2 2 2 3 3 3 3 3 3 5 5 5 1 1 1 1 1 1 5 4 4 4 4 4 4 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9

Wiltshire

The process now begins again starting with the new state of the line. It is important to show all of your working and do not skip or assume any steps. Remember to show the state of the line at the end of each iteration.

The process now begins again starting with the new state of the line. It is important to show all of your working and do not skip or assume any steps. Remember to show the state of the line at the end of each iteration.

6 comparisons, 4 swaps

5 comparisons, 2 swaps

Bubble Sort Algorithm


5th Iteration
2 2 2 2 2 3 3 1 1 1 1 1 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9

Wiltshire

Bubble Sort Algorithm


6th Iteration
2 1 1 1 1 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9

Wiltshire

The process now begins again starting with the new state of the line. It is important to show all of your working and do not skip or assume any steps. Remember to show the state of the line at the end of each iteration.

The process now begins again starting with the new state of the line. It is important to show all of your working and do not skip or assume any steps. Remember to show the state of the line at the end of each iteration.

4 comparisons, 1 swap

3 comparisons, 1 swap

10

Bubble Sort Algorithm


7th Iteration
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9

Wiltshire

Bubble Sort Algorithm


Total # of comparisons
(n-1)n/2

Wiltshire

2 comparisons, 0 swap

You will have seen from the last iteration that the final state of the line was in the correct order. You might think that this means you can stop the algorithm. Remember that an algorithm is a set or list of instructions that solve a particular problem. There is no intelligence within the algorithm to tell a computer that the correct answer has been reached. So you must run through the list again until no swap is needed. The fact that no changes are made will tell the operator that the list must be in the correct order.

Total # of swaps
(n-1)n/2

11

12

Bubble Sort Algorithm

Wiltshire

Selection with Interchange Sort Algorithm

Wiltshire

Now see if you can complete the Algorithm with the questions below. Use the bubble sort algorithm to put the following numbers in ascending order. 8 6 9 2 5 Use the bubble sort algorithm to put the following numbers in descending order. 8 6 9 2 5

In this Algorithm the smallest number is located and changed with the first.
8 3 5 2 7 9 1 4 6 1 3 5 2 7 9 8 4 6

13

14

Selection with Interchange Sort Algorithm

Wiltshire

Selection with Interchange Sort Algorithm

Wiltshire

The smallest number is now in the correct place, so you locate the second smallest and change it with the second number.
8 3 5 2 7 9 1 4 6 1 3 5 2 7 9 8 4 6 1 2 5 3 7 9 8 4 6

Then the third smallest gets swapped with the third number. The fourth smallest with the fourth number, and so on.
8 3 5 2 7 9 1 4 6 1 3 5 2 7 9 8 4 6 1 2 5 3 7 9 8 4 6 1 2 3 5 7 9 8 4 6 1 2 3 4 7 9 8 5 6 1 2 3 4 5 9 8 7 6 1 2 3 4 5 6 8 7 9 1 2 3 4 5 6 7 8 9

15

16

Selection with Interchange Sort Algorithm

Wiltshire

Selection with Interchange Sort Algorithm

Wiltshire

On the last pass you can see that the list has already been sorted. However, similar to previous algorithms all steps must be carried out to ensure the process works correctly. 8 3 5 2 7 9 1 4 6 1 3 5 2 7 9 8 4 6 1 2 5 3 7 9 8 4 6 1 2 3 5 7 9 8 4 6 1 2 3 4 7 9 8 5 6 1 2 3 4 5 9 8 7 6 1 2 3 4 5 6 8 7 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9

Now see if you can complete the Algorithm with the questions below. Use the Selection with Interchange Sort Algorithm to put the following numbers in ascending order. 4 1 6 2 5 9 Use the Selection with Interchange Sort Algorithm to put the following numbers in descending order. 4 1 6 2 5 9

17

18

Quick Sort Algorithm


8 3 5 2 7 9 1 4 6

Wiltshire

Quick Sort Algorithm


8 3 3 5 5 2 2 7 7 1 9 4 1 6 4 8 6 9

Wiltshire

The quick sort Algorithm is based on a pivot number and sub-lists. The way of obtaining a pivot number can vary. For this course we are going to use the first number in the list. Starting with your un-ordered list pick the pivot number. Here the pivot is the first number in the list, 8.

Now the pivot number is made permanent by placing everything bigger than it to the right and everything smaller to the left. This number is definitely in the correct place so it can be fixed. Notice how the order of the numbers has not changed. You now have two sub-lists either side of 8. Repeat the process of picking a pivot and sorting the list on each sub-list

19

20

Quick Sort Algorithm


8 3 2 3 5 1 5 2 3 2 7 5 7 1 7 9 4 4 1 6 6 4 8 6 9 9

Wiltshire

Quick Sort Algorithm


8 3 2 1 3 5 1 2 5 2 3 2 7 5 4 7 1 7 5 9 4 4 7 1 6 6 6 4 8 6 9 9

Wiltshire

The pivot for the left list is 3. Fix the 3 as a permanent number and put everything bigger to the right and smaller to the left. Remembering not to change the order of the numbers. Once again we are certain that the 3 is in the correct place so we can fix its position. The pivot for the right list is the 9. As this is the only number in the sub-list then it to can be made fixed.

We now have two sub-lists either side of the 3. For the left list the pivot is 2. Make it permanent. Arrange smaller numbers left and bigger numbers right. Fix 2 in final list. For the right list the pivot is 5. Make it permanent. Arrange smaller numbers left and bigger numbers right. Fix 5 in final list.

21

22

Quick Sort Algorithm


8 3 2 1 1 3 5 1 2 5 2 3 2 7 5 4 4 7 1 7 5 9 4 4 7 6 6 1 6 6 6 7 4 8 6 9 9

Wiltshire

Quick Sort Algorithm

Wiltshire

There are now three sublists. Two of them are easily dealt with because both lists are single digits. So these can be fixed in the final list. The 7 is now the pivot in the last list. Make it permanent. The 6 moves across. We can fix the 7 in the final list. Now all thats left is the 6 so we can fix that too.

Now see if you can complete the Algorithm with the questions below. Use the Selection with Quick Sort Algorithm to put the following numbers in ascending order.
9 5 7 6 11 15 10 12 3 13

Use the Selection with Quick Sort Algorithm to put the following numbers in descending order.
9 5 7 6 11 15 10 12 3 13

23

24

The Shuttle Sort Algorithm


8 3 5 2 7 9 1 4 6
Original list

Wiltshire

The Shuttle Sort Algorithm


These Thesecomparisons comparisonsare arenot not necessary! necessary! 2 3 5 8 7 9 1 4 6 2 3 5 7 8 9 1 4 6 2 3 5 7 8 9 1 4 6 2 3 5 7 8 9 1 4 6 2 3 5 7 8 9 1 4 6
4th Pass

Wiltshire

3 8 5 2 7 9 1 4 6
1st Pass

3 5 8 2 7 9 1 4 6

3 5 8 2 7 9 1 4 6
2nd Pass

3 5 2 8 7 9 1 4 6

3 2 5 8 7 9 1 4 6

2 3 5 8 7 9 1 4 6
3rd Pass

Step 1 (first pass) compare the first two numbers, Swap if necessary to put numbers in ascending order. Step 2 (second pass) compare the second and third numbers Swap if necessary. Now compare the first and second again. Swapping if necessary. Step 3 (third pass) - compare the third and fourth numbers, then the second and third and then the first and second.

2 3 5 7 8 9 1 4 6

2 3 5 8 7 8 9 1 4 6

2 3 5 7 8 9 1 4 6

2 3 5 7 8 9 1 4 6

2 3 5 7 8 9 1 4 6
5th Pass

Step 4 (4th pass) Compare the 4th and 3rd, 3rd and 2nd, 1st and 2nd. Step 5 (5th pass) Compare the 5th and 4th, 4th and 3rd, 3rd and 2nd, 1st and 2nd.

25

26

The Shuttle Sort Algorithm


2 3 5 7 8 9 1 4 6 2 3 5 7 8 1 9 4 6 2 3 5 7 1 8 9 4 6 2 3 5 1 7 8 9 4 6 2 3 1 5 7 8 9 4 6 2 1 3 5 7 8 9 4 6 1 2 3 5 7 8 9 4 6
6th Pass

Wiltshire

The Shuttle Sort Algorithm


1 2 3 5 7 8 9 4 6 1 2 3 5 7 8 4 9 6 1 2 3 5 7 4 8 9 6 1 2 3 5 4 7 8 9 6 1 2 3 4 5 7 8 9 6 1 2 3 4 5 7 8 9 6 1 2 3 4 5 7 8 9 6 1 2 3 4 5 7 8 9 6
7th Pass

Wiltshire

Step 6 (6th pass) Compare the 6th and 5th, 5th and 4th, 4th and 3rd, 3rd and 2nd, 2nd and 1st.

Step 7 (7th pass) Compare the 7th and 6th, 6th and 5th, 5th and 4th, 4th and 3rd, 3rd and 2nd, 2nd and 1st.

27

28

The Shuttle Sort Algorithm


1 2 3 4 5 7 8 9 6 1 2 3 4 5 7 8 6 9 1 2 3 4 5 7 6 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
8th Pass

Wiltshire

The Shuttle Sort Algorithm

Wiltshire

Step 8 (8th pass) Compare the 8th and 7th, 7th and 6th, 6th and 5th, 5th and 4th, 4th and 3rd, 3rd and 2nd, 2nd and 1st. Hopefully you can see that this Algorithm will always have (n-1) iterations.

Now see if you can complete the Algorithm with the questions below. Use the Selection with The Shuttle Sort Algorithm to put the following numbers in ascending order. 3 8 6 8 1 9 Use the Selection with The Shuttle Sort Algorithm to put the following numbers in descending order. 3 8 6 8 1 9

29

30

Insertion Sort Algorithm

Wiltshire

Insertion Sort Algorithm

Wiltshire

This is probably the most straight forward of the methods. Numbers should be taken one at a time from the original list, and inserted in their correct positions in the new list.
8 3 5 2 7 9 1 4 6 8 3 8 3 5 8 2 3 5 8 2 3 5 7 8 2 3 5 7 8 9 1 2 3 5 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 6 7 8 9 8 3 5 2 7 9 1 4 6

Again hopefully you can see that this will have n iterations.
8 3 8 3 5 8 2 3 5 8 2 3 5 7 8 2 3 5 7 8 9 1 2 3 5 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 6 7 8 9

31

Insertion Sort Algorithm

Wiltshire

Now see if you can complete the Algorithm with the questions below. Use the Selection with Insertion Sort Algorithm to put the following numbers in ascending order. 4 1 6 2 5 9 Use the Selection with Insertion Sort Algorithm to put the following numbers in descending order. 4 1 6 2 5 9

You might also like