1 / 28

This presentation includes custom animations.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode.

ashley
Télécharger la présentation

This presentation includes custom animations.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.

  2. Sorting Algorithms This lesson describes two famous sorting algorithms. Vocabulary ascending order bubble sort descending order selection sort sort Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  3. To sort a list of items means to organize the list based on some order such as alphabetical order or numeric order. When a list is sorted with the lowest value item at the top, the list is said to be in ascending order. 1 6 9 When a list is sorted with the highest value items at the top, the list is said to be in descending order. 9 6 1

  4. How do I sort thee? Let me count the ways. A Few Famous Sorting Algorithms Bubble sort Insertion sort Selection sort Shell sort Heap sort Merge sort Quick sort Simple and "Slow" Complex and "Fast"

  5. The selection sort works by hunting for the value that belongs in a position. Round 1 Start End 30 10 60 60 40 40 20 20 10 30 50 50

  6. The selection sort works by hunting for the value that belongs in a position. Round 2 Start End 10 10 60 20 40 40 20 60 30 30 50 50

  7. The selection sort works by hunting for the value that belongs in a position. Round 3 Start End 10 10 20 20 40 30 60 60 30 40 50 50

  8. The selection sort works by hunting for the value that belongs in a position. Round 4 Start End 10 10 20 20 30 30 60 40 40 50 50 60

  9. The selection sort works by hunting for the value that belongs in a position. Round 5 Start End 10 10 20 20 30 30 40 40 50 50 60 60

  10. Notice that at the end of each round, another value is in the correct position. Round 5 Round 4 Round 3 Round 2 Round 1

  11. If the numbers are stored in an array named, Box, how is the array declared? Click for Answer int Box[6]; Round 3 Round 2 Round 1 Round 4 Round 5

  12. How does the algorithm loop through all the positions it has to fill? Click for Answer int Box[6]; int FillMe; Round 3 Round 2 Round 1 for (FillMe= 0; FillMe<= 5; ++FillMe) { Round 4 Round 5 }

  13. How does the algorithm find the value to store in the target position? Click for Answer int Box[6]; int FillMe; int LowValue; Round 3 Round 2 Round 1 int LowIndex; for (FillMe= 0; FillMe<= 5; ++FillMe) { LowValue = Box[FillMe]; LowIndex = FillMe; for (test = FillMe+ 1; test <= 4; ++test) { if (Box[test] < LowValue) { LowValue = Box[test]; LowIndex = test; Round 4 Round 5 } } }

  14. How does the algorithm store the value in the target position without losing the value that was there originally? Click for Answer int Box[6]; int FillMe; int LowValue; int SwapValue; Round 3 Round 2 Round 1 int LowIndex; for (FillMe= 0; FillMe<= 5; ++FillMe) { LowValue = Box[FillMe]; LowIndex = FillMe; for (test = FillMe+ 1; test <= 5; ++test) { if (Box[test] < LowValue) { LowValue = Box[test]; LowIndex = test; Round 4 Round 5 } } if (FillMe!= LowIndex) { SwapValue = Box[FillMe]; Box[FillMe] = Box[LowIndex] Box[LowIndex] = SwapValue; } }

  15. Be sure you understand this algorithm completely. int Box[6]; int FillMe; int LowValue; int SwapValue; Round 3 Round 2 Round 1 int LowIndex; for (FillMe= 0; FillMe <=5; ++FillMe) { LowValue = Box[FillMe]; LowIndex = FillMe; for (test = FillMe+ 1; test <= 4; ++test) { if (Box[test] < LowValue) { LowValue = Box[test]; LowIndex = test; Round 4 Round 5 } } if (FillMe!= LowIndex) { SwapValue = Box[FillMe]; Box[FillMe] = Box[LowIndex] Box[LowIndex] = SwapValue; } }

  16. 3 2 4 5 1 3 2 4 5 1

  17. The bubble sort works by moving the low values to the top while pushing the high values to the bottom. 1 2 3 1 5 4 1 3 4 5 4 2 2 5 3 5 4 3 3 2 5 4 2 1 1 Round 1 After compare 1 After compare 3 After compare 2 After compare 4 Start

  18. The bubble sort works by moving the low values to the top while pushing the high values to the bottom. 1 3 1 2 4 5 1 3 2 5 4 3 2 5 5 1 3 2 4 4 Round 2 After compare 1 After compare 3 After compare 2 Start

  19. The bubble sort works by moving the low values to the top while pushing the high values to the bottom. 1 3 2 4 5 1 2 3 5 5 3 2 4 1 4 Round 3 After compare 1 After compare 2 Start

  20. The bubble sort works by moving the low values to the top while pushing the high values to the bottom. 1 3 2 4 5 1 3 2 4 5 Round 4 After compare 1 Start

  21. Assume the bubbles are in an array of ints named Bubble. Round 1 Round 2 Click for Answer Declare the array with 5 elements because that is how many numbers are being stored. How does the code declare the array? int Bubble[5]; Round 3 Round 4

  22. Round 1 Round 2 How does the algorithm swap 2 values? Click for Answer A holding variable holds one of the values so it isn't overrun by the new value. int Bubble[5]; int SwapValue; int test; Round 3 Round 4 SwapValue = Bubble[test]; Bubble[test] = Bubble[test + 1] Bubble[test + 1] = SwapValue;

  23. How does the algorithm determine if it should swap the 2 values? Click for Answer Notice that the element number can be the result of a calculation [test + 1] as long as it's an int. int Bubble[5]; Round 1 Round 2 int SwapValue; int test; if (Bubble[test] > Bubble[test + 1]) { SwapValue = Bubble[test]; Bubble[test] = Bubble[test + 1] Bubble[test + 1] = SwapValue; Round 3 Round 4 }

  24. How does the algorithm loop through all the pairs of bubbles in a round? Click for Answer The last test is the second to last element number in the array. int Bubble[5]; Round 1 Round 2 int SwapValue; int test; int lastTest = 3; for (test = 0; test <= lastTest; ++test) { if (Bubble[test] > Bubble[test + 1]) { SwapValue = Bubble[test]; Bubble[test] = Bubble[test + 1] Bubble[test + 1] = SwapValue; Round 3 Round 4 } }

  25. How does the algorithm loop through all the rounds? Click for Answer When the last test element number is at the top of the array, the algorithm has looked at all the possibility. int Bubble[5]; Round 1 Round 2 int SwapValue; int test; int lastTest = 3; while (lastTest > 0) { for (test = 0; test <= lastTest; ++test) { if (Bubble[test] > Bubble[test + 1]) { Round 3 Round 4 SwapValue = Bubble[test]; Bubble[test] = Bubble[test + 1] Bubble[test + 1] = SwapValue; } } --lastTest; }

  26. Can the algorithm be made more efficient? Click for Answer If there is ever a round where no values change places, then the array is fully sorted. int Bubble[5]; Round 1 Round 2 int SwapValue; int test; int lastTest = 3; int DidItSwap = 1; while ( lastTest > 0) DidItSwap != 0 && { DidItSwap = 0; for (test = 0; test <= lastTest; ++test) { if (Bubble[test] > Bubble[test + 1]) { Round 3 Round 4 SwapValue = Bubble[test]; Bubble[test] = Bubble[test + 1]; Bubble[test + 1] = SwapValue; DidItSwap = 1; } } --lastTest; }

  27. Be sure you understand this algorithm completely. int Bubble[5]; Round 1 Round 2 int SwapValue; int test; int lastTest = 3; int DidItSwap = 1; while (DidItSwap != 0 && lastTest > 0) { DidItSwap = 0; for (test = 0; test <= lastTest; ++test) { if (Bubble[test] > Bubble[test + 1]) { Round 3 Round 4 SwapValue = Bubble[test]; Bubble[test] = Bubble[test + 1] Bubble[test + 1] = SwapValue; DidItSwap = 1; } } --lastTest; }

  28. How would you change the algorithm to sort the bubbles from high to low? Click for Answer int Bubble[5]; int Bubble[5]; int SwapValue; int test; int SwapValue; int test; int lastTest = 4; int lastTest = 3; int DidItSwap = 1; int DidItSwap = 1; while (DidItSwap != 0 && lastTest > 0) while (DidItSwap != 0 && lastTest > 0) { { DidItSwap = 0; DidItSwap = 0; for (test = 0; test <= lastTest; ++test) { for (test = 0; test <= lastTest; ++test) { if (Nums[test] > Nums[test + 1]) { if (Bubble[test] Bubble[test + 1]) { < SwapValue = Bubble[test]; Bubble[test] = Bubble[test + 1] Bubble[test + 1] = SwapValue; SwapValue = Bubble[test]; Bubble[test] = Bubble[test + 1] Bubble[test + 1] = SwapValue; DidItSwap = 1; DidItSwap = 1; } } } } --lastTest; } --lastTest; }

More Related