interchange sort

32
Decision Maths Lesson 11 – Sorting Algorithms

Upload: nur-farah-anis

Post on 16-Nov-2015

54 views

Category:

Documents


1 download

DESCRIPTION

sort

TRANSCRIPT

Decision Maths

Decision Maths Lesson 11 Sorting Algorithms

WiltshireSorting AlgorithmsSomething 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, 6Clearly 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 AlgorithmSelection with interchange Sort AlgorithmQuick Sort AlgorithmShuttle Sort AlgorithmInsertion Sort Algorithm

Wiltshire835279146385279146358279146352879146352789146352789146352781946352781496352781469Bubble Sort Algorithm

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.

1st Iteration

WiltshireBubble Sort Algorithm

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.

352781469352781469325781469325781469325781469325718469325714869325714689325714689

2nd Iteration

Wiltshire235714689523714689723514689123574689423517689623514789823514679923514678235146789Bubble Sort Algorithm

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.

3rd Iteration

WiltshireBubble Sort Algorithm

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

4th Iteration

WiltshireBubble Sort Algorithm

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

5th Iteration

WiltshireBubble Sort Algorithm

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

6th Iteration

WiltshireBubble Sort Algorithm

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 one last time.The fact that no changes are made will tell the operator that the list must be in the correct order.

213456789321456789421356789521346789621345789721345689821345679892134567213456789

7th Iteration

WiltshireBubble Sort AlgorithmNow 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 6925Use the bubble sort algorithm to put the following numbers in descending order.8 6925

WiltshireSelection with InterchangeSort AlgorithmIn this Algorithm the smallest number is located and changed with the first.835279146135279846

WiltshireSelection with InterchangeSort AlgorithmThe smallest number is now in the correct place, so you locate the second smallest and change it with the second number.835279146135279846125379846

WiltshireSelection with InterchangeSort AlgorithmThen the third smallest gets swapped with the third number.The fourth smallest with the fourth number, and so on.835279146135279846125379846123579846123479856123459876123456879

123456789

WiltshireSelection with InterchangeSort AlgorithmOn 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. 835279146135279846125379846123579846123479856123459876123456879123456789123456789

WiltshireSelection with InterchangeSort AlgorithmNow see if you can complete the Algorithm with the questions below.Use the Selection with InterchangeSort Algorithm to put the following numbers in ascending order.4 16259Use the Selection with InterchangeSort Algorithm to put the following numbers in descending order.4 16259

WiltshireThe quick sort Algorithm is based on a pivot number and sub-lists.The way of obtaining a pivot number can vary. In the Edexcel course they use a middle number (see very last slide).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.

Quick Sort Algorithm835279146

WiltshireNow 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-listQuick Sort Algorithm835279146

8

352714698

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

Quick Sort Algorithm835279146

9

83

983

35271469125746

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

4

1

574

Quick Sort Algorithm835279146

1

92

83

5

98532

352714692676

WiltshireThere are now three sub-lists.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.

4

7

1

574

Quick Sort Algorithm835279146

1

197

2

84

36

5

987654321

352714692666

WiltshireQuick Sort AlgorithmNow 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.4 16259Use the Selection with Quick Sort Algorithm to put the following numbers in descending order.4 16259

WiltshireThe Shuttle Sort AlgorithmStep 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.8352791463852791463582791463582791463528791463258791461st Pass2nd Pass3rd Pass838535823225235879146Originallist

WiltshireThe Shuttle Sort AlgorithmStep 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.

2358791462357891462357891464th Pass2357891462357891465th Pass235789146235889146235789146235789146235789146873575352332895787

WiltshireThe Shuttle Sort AlgorithmStep 6 (6th pass)Compare the 6th and 5th, 5th and 4th, 4th and 3rd, 3rd and 2nd, 2nd and 1st.

2357891466thPass235781946235718946235178946231578946213578946917118513112123578946

WiltshireThe Shuttle Sort AlgorithmStep 7 (7th pass)Compare the 7th and 6th, 6th and 5th, 5th and 4th, 4th and 3rd, 3rd and 2nd, 2nd and 1st.7th Pass12357489612357894612357849612354789612345789612345789612345789694744854341232123457896

WiltshireThe Shuttle Sort AlgorithmStep 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.

1234578968thPass1234578691234576891234567891234567891234567891234567891234567891234567899676681256452343

WiltshireThe Shuttle Sort AlgorithmNow 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 86819Use the Selection with The Shuttle Sort Algorithm to put the following numbers in descending order.3 86819

WiltshireInsertion Sort AlgorithmThis 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.8352791468

38

358

2358

23578

235789

1235789

12345789

123456789

WiltshireInsertion Sort AlgorithmAgain hopefully you can see that this will have n iterations.8352791468

38

358

2358

23578

235789

1235789

12345789

123456789

WiltshireInsertion Sort AlgorithmNow 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 16259Use the Selection with Insertion Sort Algorithm to put the following numbers in descending order.4 16259

WiltshireComplexityBubble SortSelection with interchangeQuick SortShuttle SortInsertion Sort

Wiltshire33546

35246

89352146

Edexcel Quick Sort Algorithm835279146

8

197

2

84

36

5

987654321565

Wiltshire