1 / 29

Computer Science 101

Computer Science 101. Introduction to Sorting. Sorting. One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical order for purposes of Reports by category Summarizing data Searching data. Sorting.

dean
Télécharger la présentation

Computer Science 101

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. Computer Science 101 Introduction to Sorting

  2. Sorting • One of the most common activities of a computer is sorting data • Arrange data into numerical or alphabetical order for purposes of • Reports by category • Summarizing data • Searching data

  3. Sorting • We’re given a list of data in random order • At the end of the sorting, each datum is less than or equal to its successor in the list • For each i from 1 to N - 1, A(i) <= A(i + 1)

  4. Visualizing the Results List A Size = 8 34 22 66 80 14 90 32 16 Sorting Algorithm 14 16 22 32 34 66 80 90 Sorting algorithms usually move data around in the original list

  5. First Algorithm: Selection Sort • Strategy: • Find the largest item in the list and exchange it with the last item in the list • Find the next largest item in the list and exchange it with the next to the last item in the list • Etc. 34 22 66 80 14 90 32 16 Largest Upper

  6. First Algorithm: Selection Sort • Strategy: • Find the largest item in the list and exchange it with the last item in the list • Find the next largest item in the list and exchange it with the next to the last item in the list • Etc. 34 22 66 80 14 16 32 90 Largest Upper

  7. First Algorithm: Selection Sort • Strategy: • Find the largest item in the list and exchange it with the last item in the list • Find the next largest item in the list and exchange it with the next to the last item in the list • Etc. 34 22 66 32 14 16 80 90 Largest Upper

  8. First Algorithm: Selection Sort • Strategy: • Find the largest item in the list and exchange it with the last item in the list • Find the next largest item in the list and exchange it with the next to the last item in the list • Etc. 34 22 16 32 14 66 80 90 Largest Upper

  9. First Algorithm: Selection Sort • Strategy: • Find the largest item in the list and exchange it with the last item in the list • Find the next largest item in the list and exchange it with the next to the last item in the list • Etc. 14 22 16 32 34 66 80 90 Largest Upper

  10. First Algorithm: Selection Sort • Strategy: • Find the largest item in the list and exchange it with the last item in the list • Find the next largest item in the list and exchange it with the next to the last item in the list • Etc. 14 22 16 32 34 66 80 90 Largest Upper

  11. First Algorithm: Selection Sort • Strategy: • Find the largest item in the list and exchange it with the last item in the list • Find the next largest item in the list and exchange it with the next to the last item in the list • Etc. 14 16 22 32 34 66 80 90 Largest Upper

  12. First Algorithm: Selection Sort • Strategy: • Find the largest item in the list and exchange it with the last item in the list • Find the next largest item in the list and exchange it with the next to the last item in the list • Etc. 14 16 22 32 34 66 80 90 Upper Largest

  13. First Algorithm: Selection Sort • Uses an algorithm we’ve already seen, search for the largest, as a component A The list N The size of the list Upper The end of the unsorted portion of the list Largest The position of the largest item so far Current Used to traverse the list during a search

  14. The Selection Sort Algorithm set Upper to N while Upper > 1 do set Largest to the position of the largest item between positions 1 and Upper exchange A(Largest) and A(Upper) decrement Upper The component in red is the search for largest algorithm We’ll expand that into detailed form next Note for now that the code in red executes N – 1 times.

  15. The Selection Sort Algorithm set Upper to N while Upper > 1 do set Largest 1 set Current to 2 while Current <= Upper do if A(Current) > A(Largest) then set Largest to Current increment current exchange A(Largest) and A(Upper) decrement Upper The code in red is the complete search for largest algorithm Note that the first search requires N – 1 comparisons and each remaining search requires one less comparison

  16. The Selection Sort Algorithm set Upper to N while Upper > 1 do set Largest 1 set Current to 2 while Current <= Upper do if A(Current) > A(Largest) then set Largest to Current increment current exchange A(Largest) and A(Upper) decrement Upper The total number of comparisons = (N2 – N) / 2 The total number of exchanges = N - 1

  17. Improving Selection Sort set Upper to N while Upper > 1 do set Largest 1 set Current to 2 while Current <= Upper do if A(Current) > A(Largest) then set Largest to Current increment current if Largest  Upper then exchange A(Largest) and A(Upper) decrement Upper Some exchanges might not be necessary What is the best case? the worst case?

  18. 2nd Algorithm: Bubble Sort • Strategy: • Pass through the list from left to right • Compare each element with the one to its left • Swap them if they are out of order • Such a pass will put largest at the right end • Continue making these passes until sorted 34 22 66 80 14 90 32 16 Current

  19. 2nd Algorithm: Bubble Sort • Strategy: • Pass through the list from left to right • Compare each element with the one to its left • Swap them if they are out of order • Such a pass will put largest at the right end • Continue making these passes until sorted 22 34 66 80 14 90 32 16 Current

  20. 2nd Algorithm: Bubble Sort • Strategy: • Pass through the list from left to right • Compare each element with the one to its left • Swap them if they are out of order • Such a pass will put largest at the right end • Continue making these passes until sorted 22 34 66 80 14 90 32 16 Current

  21. 2nd Algorithm: Bubble Sort • Strategy: • Pass through the list from left to right • Compare each element with the one to its left • Swap them if they are out of order • Such a pass will put largest at the right end • Continue making these passes until sorted 22 34 66 80 14 90 32 16 Current

  22. 2nd Algorithm: Bubble Sort • Strategy: • Pass through the list from left to right • Compare each element with the one to its left • Swap them if they are out of order • Such a pass will put largest at the right end • Continue making these passes until sorted 22 34 66 14 80 90 32 16 Current

  23. 2nd Algorithm: Bubble Sort • Strategy: • Pass through the list from left to right • Compare each element with the one to its left • Swap them if they are out of order • Such a pass will put largest at the right end • Continue making these passes until sorted 22 34 66 14 80 90 32 16 Current

  24. 2nd Algorithm: Bubble Sort • Strategy: • Pass through the list from left to right • Compare each element with the one to its left • Swap them if they are out of order • Such a pass will put largest at the right end • Continue making these passes until sorted 22 34 66 14 80 32 90 16 Current

  25. 2nd Algorithm: Bubble Sort • Strategy: • Pass through the list from left to right • Compare each element with the one to its left • Swap them if they are out of order • Such a pass will put largest at the right end • Continue making these passes until sorted 22 34 66 14 80 32 16 90 Current

  26. The Bubble Sort Algorithm set Upper to N while Upper > 1 do bubble the largest item down to upper decrement Upper Note that the bubble process is executed N – 1 times

  27. The Bubble Sort Algorithm set Upper to N while Upper > 1 do set Current to 2 while Current <= Upper do if A(Current) < A(Current – 1) then exchange A(Current) and A(Current – 1) increment Current decrement Upper Note that the bubble process is executed N – 1 times Note that the first bubble process requires N – 1 comparisons and each remaining bubble process needs one less comparison How many total comparisons? Exchanges?

  28. Improving Bubble Sort set Upper to N while Upper > 1 do set Current to 2 while Current <= Upper do if A(Current) < A(Current – 1) then exchange A(Current) and A(Current – 1) increment Current decrement Upper If no exchanges are performed during a bubble process, then the list is sorted Perhaps we can quit the outer loop when this is the case

  29. Improving Bubble Sort set Upper to N Set Sorted to false while Upper > 1 and not Sorted do set Current to 2 set Sorted to true while Current <= Upper do if A(Current) < A(Current – 1) then set Sorted to false exchange A(Current) and A(Current – 1) increment Current decrement Upper We assume that the list is sorted before each bubble process and prove that it’s not sorted when an exchange is made What is the best case # of comparisons now?

More Related