Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.

Similar presentations


Presentation on theme: "Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front."— Presentation transcript:

1 Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front of the list Repeat by finding the minimum in the sublist On the i th iteration, find the i th smallest number in the list Keep going until you reach the end of the list 4 Copyright © William C. Cheng

2 5 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort First pass through the list 10719516

3 Data Structures - CSCI 102 Selection Sort First pass through the list 10719516 Minimum 6 Copyright © William C. Cheng

4 7 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort First pass through the list 10719516 MinimumOutput

5 Data Structures - CSCI 102 Selection Sort First pass through the list 10719516 Swap 8 Copyright © William C. Cheng

6 9 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort First pass through the list 10 5 7777 19 5 10 16 UnsortedSorted

7 Data Structures - CSCI 102 Selection Sort 2nd pass through the list 57191016 Minimum 10 Copyright © William C. Cheng

8 Data Structures - CSCI 102 Selection Sort 2nd pass through the list 57191016 Minimum 57191016 Stay 11 Copyright © William C. Cheng

9 12 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 2nd pass through the list 57191016 Minimum 57191016 Stay 57191016 SortedUnsorted

10 13 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 3rd pass through the list 57191016 MinimumOutput

11 Data Structures - CSCI 102 Selection Sort 3rd pass through the list 57191016 Minimum 57191016 Output Swap 14 Copyright © William C. Cheng

12 15 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 3rd pass through the list 57191016 Minimum 57191016 Output 57101916 SortedUnsorted Swap

13 16 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 4th pass through the list 57101916 MinimumOutput

14 Data Structures - CSCI 102 Selection Sort 4th pass through the list 57101916 Minimum 57101916 Output Swap 17 Copyright © William C. Cheng

15 18 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 4th pass through the list 57101916 Minimum 57101916 Output 57101619 SortedUnsorted Swap

16 19 Copyright © William C. Cheng Data Structures - CSCI 102 Selection Sort 5th pass through the list 57101619 No 5th pass needed The last element must be sorted by now 57101619

17 20 Data Structures - CSCI 102 Copyright © William C. Cheng Selection Sort void selectionSort(vector & numbers) { for(int i=0; i < numbers.size()-1; i++) { //find the minimum int minValue = numbers[i] int minIndex = i; for(int j=i+1; j < numbers.size(); j++) { if(numbers[j] < minValue) { minValue = numbers[j]; minIndex = j; }}}} //move the minimum into the sorted section swap(numbers[i],numbers[minIndex]); }}}}

18 None, O(n ) O(n ) What’s the best case scenarios? What’s the Big O for this case? Selection Sort 22 Data Structures - CSCI 102 Copyright © William C. Cheng Selection Sort What’s the worst case scenario? What’s the Big O for this case? What’s the overall Big O? What are the problems with selection sort? 2 2 2 Still too many comparisons

19 Bubble Sort 23 Data Structures - CSCI 102 Copyright © William C. Cheng Bubble Sort Compare adjacent numbers If they’re out of order swap them Otherwise leave them be Loop through the list until there are no more swaps to make Smaller numbers slowly "bubble" up toward the top of the list

20 24 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list 10719516

21 Data Structures - CSCI 102 Bubble Sort First pass through the list 10719516 Swap 25 Copyright © William C. Cheng

22 26 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list 10719516 Swap 71019516

23 27 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list 10 7 10 19 5555 16

24 Data Structures - CSCI 102 Bubble Sort First pass through the list Stay 28 Copyright © William C. Cheng 10 7 10 19 5555 16

25 29 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list 10 7 10 19 5555 16

26 Data Structures - CSCI 102 Bubble Sort First pass through the list Swap 30 Copyright © William C. Cheng 10 7 10 19 5555 16

27 31 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list Swap 10 7 10 19 5555 16 71051916

28 32 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list 10 7 10 19 5 19 16

29 Data Structures - CSCI 102 Bubble Sort First pass through the list Swap 33 Copyright © William C. Cheng 10 7 10 19 5 19 16

30 34 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list Swap 10 7 10 19 5 19 16 71051619

31 35 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list 10 7 10 19 5 19 16 19

32 36 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort First pass through the list 10 7 10 19 5 19 16 19

33 37 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list 71051619

34 Data Structures - CSCI 102 Bubble Sort 2nd pass through the list 71051619 Stay 38 Copyright © William C. Cheng

35 39 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list 71051619

36 Data Structures - CSCI 102 Bubble Sort 2nd pass through the list 71051619 Swap 40 Copyright © William C. Cheng

37 41 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list 71051619 Swap 75101619

38 42 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list 7777 10 5 10 16 19

39 Data Structures - CSCI 102 Bubble Sort 2nd pass through the list Stay 43 Copyright © William C. Cheng 7777 10 5 10 16 19

40 44 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list 7777 10 5 10 16 19

41 45 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 2nd pass through the list 7777 10 5 10 16 19

42 46 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 3rd pass through the list 75101619

43 Data Structures - CSCI 102 Bubble Sort 3rd pass through the list 75101619 Swap 47 Copyright © William C. Cheng

44 48 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 3rd pass through the list 75101619 57101619 Swap

45 49 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 3rd pass through the list 7575 5757 10 16 19

46 Data Structures - CSCI 102 Bubble Sort 3rd pass through the list 7575 5757 10 16 19 Stay 50 Copyright © William C. Cheng

47 51 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 3rd pass through the list 7575 5757 10 16 19

48 52 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 4th pass through the list 57101619

49 Data Structures - CSCI 102 Bubble Sort 4th pass through the list 57101619 Stay 53 Copyright © William C. Cheng

50 54 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 4th pass through the list 57101619

51 55 Copyright © William C. Cheng Data Structures - CSCI 102 Bubble Sort 4th pass through the list 57101619 57101619 During the 4th pass, nothing got swapped This means that the unsorted part is actually sorted No more pass needed

52 56 Data Structures - CSCI 102 Copyright © William C. Cheng Bubble Sort void naiveBubbleSort(vector & numbers) { for(int i=0; i < numbers.size()-1; i++) { for(int j=0; j < numbers.size()-1; j++) { if(numbers[j] > numbers[j+1]) { swap(numbers[j],numbers[j+1]); }}}}}}}} Naive Bubble Sort

53 BubbleSort(n) = O(n ) 2) Take advantage of the fact that on the i iteration, 58 Data Structures - CSCI 102 Copyright © William C. Cheng Bubble Sort Naive Bubble Sort What’s the Big O for this bubble sort? How could we improve this algorithm? 2 1) Make it terminate early if the sorting is already done th the last i elements are sorted

54 List in Reverse Order, O(n ) O(n ) 61 Data Structures - CSCI 102 Copyright © William C. Cheng Bubble Sort Better Bubble Sort What’s the overall Big O? What are the problems with bubble sort? What’s the best case scenarios? What’s the Big O for this case? What’s the worst case scenario? What’s the Big O for this case? Already Sorted, O(n) 2 2 Too much swapping! Too many comparisons!

55 On the i iteration, i items will be sorted Keep the list separated into sorted and unsorted sections Insertion Sort 62 Data Structures - CSCI 102 Copyright © William C. Cheng Insertion Sort Take each number in the unsorted portion of the list and shift it over into the sorted list th Keep going until you reach the end of the list "Bubble" to the left until finding the right place

56 63 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort First pass through the list 10719516 SortedUnsorted

57 Data Structures - CSCI 102 Insertion Sort First pass through the list 10719516 Swap 64 Copyright © William C. Cheng

58 65 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort First pass through the list 10 7 10 19 5555 16

59 66 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 2nd pass through the list 71019516 SortedUnsorted

60 67 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 2nd pass through the list 71019516

61 Data Structures - CSCI 102 Insertion Sort 2nd pass through the list 71019516 Stay 68 Copyright © William C. Cheng

62 69 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 2nd pass through the list 7777 10 19 5555 16

63 70 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 3rd pass through the list 71019516 SortedUnsorted

64 71 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 3rd pass through the list 71019516

65 Data Structures - CSCI 102 Insertion Sort 3rd pass through the list 71019516 Swap 72 Copyright © William C. Cheng

66 73 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 3rd pass through the list 7777 10 19 5 19 16

67 Data Structures - CSCI 102 Insertion Sort 3rd pass through the list 7777 10 19 5 19 16 Swap 74 Copyright © William C. Cheng

68 75 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 3rd pass through the list 777777 10 5 19 5 10 5 19 16

69 Data Structures - CSCI 102 Insertion Sort 3rd pass through the list 777777 10 5 19 5 10 5 19 16 Swap 76 Copyright © William C. Cheng

70 77 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 3rd pass through the list 77757775 10 5 7 19 5 10 5 19 16

71 57101916 78 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 4th pass through the list SortedUnsorted

72 57101916 Data Structures - CSCI 102 Insertion Sort 4th pass through the list Swap 79 Copyright © William C. Cheng

73 80 Copyright © William C. Cheng Data Structures - CSCI 102 Insertion Sort 4th pass through the list 5555 7777 10 19 16 19

74 Data Structures - CSCI 102 Insertion Sort 4th pass through the list 5555 7777 10 19 16 19 Stay 81 Copyright © William C. Cheng

75 82 Data Structures - CSCI 102 Insertion Sort 4th pass through the list 555555 777777 10 19 16 19 Too many swaps! Since the left part is sorted, just need to find a place to insert the value (16 in this case) Need to remember the value 16 Copyright © William C. Cheng

76 In Reverse Order, O(n ) O(n ) 115 Data Structures - CSCI 102 Copyright © William C. Cheng Insertion Sort What’s the best case scenarios? What’s the Big O for this case? Already Sorted, O(n) What’s the worst case scenario? What’s the Big O for this case? 2 What’s the overall Big O? What are the problems with insertion sort? 2 Still have to use nested loops

77 Divide 4 Data Structures - CSCI 102 Copyright © William C. Cheng Merge Sort Divide the N-element sequence to be sorted into two subsequences of N/2 elements each Conquer Sort the two subsequences recursively using merge sort Combine Merge the two sorted subsequences to produce the a single sorted result. Repeat.

78 5 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort First pass through the list 357810141619

79 6 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 571019381416 357810141619 Divide

80 7 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 571019381416

81 8 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 571019381416 710519816314 Divide

82 9 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 710519816314

83 10 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 107195168143 710519816314 Divide

84 11 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 107195168143

85 12 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 107195168143

86 13 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 107 7 195 5 168 8 143 3 Merge

87 14 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 107 7 195 5 168 8 143 3 Merge

88 15 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 710519816314

89 16 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 710519 571019 816314 38 16 Merge

90 17 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 571019381416

91 18 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 571019381416 357810141619 Merge

92 19 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort 357810141619

93 20 Data Structures - CSCI 102 Merge Sort What functions will we need to do merge sort? Need a function to split list in half Need a function to merge two sorted lists Most of the work in the merge sort algorithm is done in the "merge" method Precondition: Given two sorted arrays Postcondition: Need a single, merged sorted array How does it work? Please note that Merge Sort is not an in-place sorting algorithm It uses an output buffer whose size is on the order of the size of the input buffer, i.e., O(n) extra space Merge Sort may not be practical for some applications Copyright © William C. Cheng

94 21 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist

95 22 Copyright © William C. Cheng 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist Data Structures - CSCI 102 Merge Sort (Merge Algorithm) Right is smaller Copy right to output

96 23 Copyright © William C. Cheng 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 3

97 24 Copyright © William C. Cheng 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 3

98 25 Copyright © William C. Cheng 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 3 Left is smaller Copy left to output

99 26 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 35

100 27 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 35

101 28 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 35 Left is smaller Copy left to output

102 29 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 357

103 30 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 357

104 31 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 357 Right is smaller Copy right to output

105 32 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 3578

106 33 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 3578

107 34 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 3578 Left is smaller Copy left to output

108 35 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 357810

109 36 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 357810

110 37 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 357810 Right is smaller Copy right to output

111 38 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 35781014

112 39 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 35781014

113 40 Copyright © William C. Cheng Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 35781014 Right is smaller Copy right to output 16

114 Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 3 8 14 16 Left Sublist Right Sublist 41 Copyright © William C. Cheng 3578101416

115 Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 3 8 14 16 3578101416 Left Sublist Right Sublist Copy all the remaining items from the left sublist to the output 42 Copyright © William C. Cheng

116 Data Structures - CSCI 102 Merge Sort (Merge Algorithm) 5 7 10 19 Left Sublist 3 8 14 16 Right Sublist 357810141619 Copy all the remaining items from the left sublist to the output Done 43 Copyright © William C. Cheng

117 What’s the best case scenarios? What’s the Big O for this case? Merge Sort 47 Data Structures - CSCI 102 Copyright © William C. Cheng Merge Sort What’s the worst case scenario? What’s the Big O for this case? What’s the overall Big O? What are the problems with merge sort? None, O(n log n) O(n log n)

118 Stable Sort (generally) Maintains original ordering of equal elements The Good 48 Data Structures - CSCI 102 Copyright © William C. Cheng Merge Sort Fairly easy to code Useful for lists that must be accessed sequentially e.g. Linked Lists Sorting in place is really difficult The Bad O(N) additional space complexity if not in place There are better in-place sorts Lots of copying data

119 51 Data Structures - CSCI 102 Copyright © William C. Cheng Quick Sort Pick an element in the array and call it Pivot Divide Split the rest of the array into two subarrays: One array containing all numbers <= Pivot One array containing all numbers > Pivot Sort the two new arrays recursively using quick sort Conquer The arrays are sorted in place. No recombination necessary. Combine We will simply use the last element

120 52 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort The idea behind the Divide part 41852637

121 41852637 Data Structures - CSCI 102 Quick Sort The idea behind the Divide part 1) Choose the last element in the array as the pivot It doesn’t move (until the very end) So, we will pin it where it’s at for now 53 Copyright © William C. Cheng

122 Data Structures - CSCI 102 Quick Sort The idea behind the Divide part 41852637 Pivot 1) Choose the last element in the array as the pivot 2) Identify all the numbers smaller than the pivot (in blue) Identify all the numbers larger than the pivot (in orange) 54 Copyright © William C. Cheng

123 Data Structures - CSCI 102 Quick Sort The idea behind the Divide part 41852637 Pivot 1) Choose the last element in the array as the pivot 2) Identify all the numbers smaller than the pivot (in blue) Identify all the numbers larger than the pivot (in orange) 3) Move all the numbers smaller than the pivot to the left Move all the numbers larger than the pivot to the right 55 Copyright © William C. Cheng

124 Data Structures - CSCI 102 Quick Sort The idea behind the Divide part 41852637 Pivot 1) Choose the last element in the array as the pivot 2) Identify all the numbers smaller than the pivot (in blue) Identify all the numbers larger than the pivot (in orange) 3) Move all the numbers smaller than the pivot to the left Move all the numbers larger than the pivot to the right 4) Move the pivot between the blue and the orange numbers 56 Copyright © William C. Cheng

125 57 Data Structures - CSCI 102 Quick Sort The idea behind the Divide part 41852637 final sort order Copyright © William C. Cheng Pivot 1) Choose the last element in the array as the pivot 2) Identify all the numbers smaller than the pivot (in blue) Identify all the numbers larger than the pivot (in orange) 3) Move all the numbers smaller than the pivot to the left Move all the numbers larger than the pivot to the right 4) Move the pivot between the blue and the orange numbers The pivot is now at the correct position in the

126 58 Data Structures - CSCI 102 Quick Sort The idea behind the Divide part 41852637 final sort order Copyright © William C. Cheng Pivot QuickSort this 1) Choose the last element in the array as the pivot 2) Identify all the numbers smaller than the pivot (in blue) Identify all the numbers larger than the pivot (in orange) 3) Move all the numbers smaller than the pivot to the left Move all the numbers larger than the pivot to the right 4) Move the pivot between the blue and the orange numbers The pivot is now at the correct position in the

127 59 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort How to do it? 41852637

128 Data Structures - CSCI 102 Quick Sort How to do it? 41852637 Pivot Step (1) is easy 60 Copyright © William C. Cheng

129 Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 41852637 PivotUnsorted Keep array split into 3 sections: Numbers less than or equal to the pivot (<= 4, in blue) Numbers greater than the pivot (> 4, in orange) Numbers yet to be sorted 65 Copyright © William C. Cheng

130 Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 41852637 PivotUnsorted Keep array split into 3 sections: Numbers less than or equal to the pivot (<= 4, in blue) Numbers greater than the pivot (> 4, in orange) Numbers yet to be sorted 66 Copyright © William C. Cheng

131 Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 41852637 PivotUnsortedLess Than Pivot Keep array split into 3 sections: Numbers less than or equal to the pivot (<= 4, in blue) Numbers greater than the pivot (> 4, in orange) Numbers yet to be sorted 67 Copyright © William C. Cheng

132 Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 41852637 PivotUnsorted Keep array split into 3 sections: Numbers less than or equal to the pivot (<= 4, in blue) Numbers greater than the pivot (> 4, in orange) Numbers yet to be sorted 68 Copyright © William C. Cheng Less Than Pivot More Than Pivot

133 Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 41852637 PivotUnsorted Keep array split into 3 sections: Numbers less than or equal to the pivot (<= 4, in blue) Numbers greater than the pivot (> 4, in orange) Numbers yet to be sorted 69 Copyright © William C. Cheng Less Than Pivot More Than Pivot

134 Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 41852637 PivotUnsortedLess Than Pivot More Than Pivot Swap 70 Copyright © William C. Cheng

135 71 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 48152637 PivotUnsortedLess Than Pivot More Than Pivot

136 Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 48152637 PivotUnsortedLess Than Pivot More Than Pivot Swap 72 Copyright © William C. Cheng

137 73 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 48152673 PivotUnsortedLess Than Pivot More Than Pivot

138 74 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 48152673 Pivot Unsorted Less Than Pivot More Than Pivot

139 75 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 48152673 PivotLess Than Pivot More Than Pivot

140 Data Structures - CSCI 102 Quick Sort How to do steps (2) and (3) together? 48152673 PivotLess Than Pivot More Than Pivot Note: we only swap when the number is < pivot When the number is > pivot, we just move along and did nothing 76 Copyright © William C. Cheng

141 Data Structures - CSCI 102 Quick Sort Step (4) 48152673 PivotLess Than Pivot More Than Pivot Swap 77 Copyright © William C. Cheng

142 78 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Step (4) 84152673 PivotLess Than Pivot More Than Pivot

143 Data Structures - CSCI 102 Quick Sort Step (4) 84152673 PivotLess Than Pivot More Than Pivot What if some other cell has the same value as the pivot? Does that cell belong to the blue or the orange section? 79 Copyright © William C. Cheng

144 Data Structures - CSCI 102 Quick Sort Step (4) 84152673 PivotLess Than Pivot More Than Pivot What if some other cell has the same value as the pivot? Does that cell belong to the blue or the orange section? The answer is: it does not matter if it is done consistently Arbitrarily, we use: blue is for numbers  pivot 80 Copyright © William C. Cheng

145 81 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively 84152673 1238567

146 82 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively 84152673 1238567

147 83 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively 84152673 1238567 12567

148 84 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively 84152673 1238567 21675

149 85 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively 84152673 1238567 21675 257

150 86 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort Sort Recursively 84152673 1238567 21675 257

151 87 Copyright © William C. Cheng Data Structures - CSCI 102 Quick Sort All done! No merge! 84152673 Sort Recursively 1238567 21675 257

152 Sorted or reverse sorted, O(n ) O(n ), but O(n log n) on the average 1) certain inputs can be O(n ) Quick Sort 92 Data Structures - CSCI 102 Copyright © William C. Cheng Quick Sort What’s the best case scenarios? What’s the Big O for this case? What’s the worst case scenario? What’s the Big O for this case? What’s the overall Big O? What are the problems with quick sort? Every partition splits list in half, O(n log n) 2 2 2 2) slow on small lists

153 116 Data Structures - CSCI 102 Copyright © William C. Cheng Extra Material What do sorting algorithms sound like? http://flowingdata.com/2010/09/01/what-different- sorting-algorithms-sound-like/ http://www.math.ucla.edu/~rcompton/musical_sorting_ algorithms/musical_sorting_algorithms.html http://www.youtube.com/usccs102 CSCI 102 YouTube channel Sorting Algorithm Animations http://www.sorting-algorithms.com/


Download ppt "Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front."

Similar presentations


Ads by Google