Presentation is loading. Please wait.

Presentation is loading. Please wait.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.

Similar presentations


Presentation on theme: "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."— 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 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 This lesson describes two famous sorting algorithms. Vocabulary ascending order bubble sort descending order selection sort sort

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. When a list is sorted with the highest value items at the top, the list is said to be in descending order. 169169 961961

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

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

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

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

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

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

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

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

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

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

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

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

16 3 2 4 5 1 32451

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

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

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

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

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

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

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

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

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

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

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

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


Download ppt "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."

Similar presentations


Ads by Google