+ All Categories
Home > Documents > D1,L2 Sorting Algorithms.ppt

D1,L2 Sorting Algorithms.ppt

Date post: 12-Nov-2015
Category:
Upload: mokhtarppg
View: 216 times
Download: 1 times
Share this document with a friend
17
Decision Maths Sorting Algorithms
Transcript
  • Decision Maths Sorting Algorithms

  • Sorting 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.

  • Bubble 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

  • Bubble 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.

    2nd Iteration

  • Bubble 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

  • Bubble 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.4th Iteration

  • Bubble 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.5th Iteration

  • Bubble 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.6th Iteration

  • Bubble 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.

    7th Iteration

  • Bubble 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

  • Quick Sort AlgorithmThe 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 we use a middle number (see very last slide).For this example 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 AlgorithmNow 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-list898

  • Quick Sort AlgorithmThe 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.9839839

  • Quick Sort AlgorithmWe 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.

    4157419283598532926

  • Quick Sort AlgorithmThere 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.157411928436598765432192666

  • Edexcel Quick Sort Algorithm38192843659876543215

  • Quick 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

    Decision Maths - Sorting Algorithms


Recommended