Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 3422668014903216 Sorting Algorithm List A Size = 8 1416223234668090 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. 3422668014903216 LargestUpper

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. 3422668014163290 LargestUpper

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. 3422663214168090 LargestUpper

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. 3422163214668090 LargestUpper

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. 1422163234668090 LargestUpper

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. 1422163234668090 LargestUpper

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. 1416223234668090 LargestUpper

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. 1416223234668090 LargestUpper

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 = (N 2 – 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 3422668014903216 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 2234668014903216 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 2234668014903216 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 2234668014903216 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 2234661480903216 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 2234661480903216 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 2234661480329016 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 2234661480321690 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?


Download ppt "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."

Similar presentations


Ads by Google