Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.

Similar presentations


Presentation on theme: "CS1022 Computer Programming & Principles Lecture 2.2 Algorithms."— Presentation transcript:

1 CS1022 Computer Programming & Principles Lecture 2.2 Algorithms

2 Plan of lecture Examples of sorting algorithms 2 CS1022

3 Bubble Sort

4 Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 5 12 35 42 77101 1 2 3 4 5 6

5 "Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end –“ Bubble ” the largest value to the end using pair-wise comparisons and swapping 5 12 3542 77 101 1 2 3 4 5 6

6 "Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end –“ Bubble ” the largest value to the end using pair-wise comparisons and swapping 5 12 3542 77 101 Swap 4277 1 2 3 4 5 6

7 "Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end –“ Bubble ” the largest value to the end using pair-wise comparisons and swapping 5 12 3577 42 101 Swap 3577 1 2 3 4 5 6

8 "Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end –“ Bubble ” the largest value to the end using pair-wise comparisons and swapping 5 12 7735 42 101 Swap 1277 1 2 3 4 5 6

9 "Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end –“ Bubble ” the largest value to the end using pair-wise comparisons and swapping 5 77 1235 42 101 No need to swap 1 2 3 4 5 6

10 "Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end –“ Bubble ” the largest value to the end using pair-wise comparisons and swapping 5 77 1235 42 101 Swap 5101 1 2 3 4 5 6

11 "Bubbling Up" the Largest Element Traverse a collection of elements – Move from the front to the end –“ Bubble ” the largest value to the end using pair-wise comparisons and swapping 77 1235 42 5 101 Largest value correctly placed 1 2 3 4 5 6

12 The “ Bubble Up ” Algorithm begin index <- 1 last_compare_at <- n – 1 while not(index > last_compare_at) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) index <- index + 1 end

13 No, Swap isn ’ t built in. begin Swap(a, b) t <- a a <- b b <- t end

14 Items of Interest Notice that only the largest value is correctly placed All other values are still out of order So we need to repeat this process 77 1235 42 5 1 2 3 4 5 6 101 Largest value correctly placed

15 Repeat “ Bubble Up ” How Many Times? If we have N elements… And if each time we bubble an element, we place it in its correct location… Then we repeat the “ bubble up ” process N – 1 times. This guarantees we ' ll correctly place all N elements.

16 “ Bubbling ” All the Elements 77 1235 42 5 101 5 4212 35 77 10142 5 35 12 77 10142 35 5 12 77 10142 35 12 5 77 101 N - 1 1 2 3 4 5 6

17 Reducing the Number of Comparisons 12 35 42 77 101 577 1235 42 5 101 5 4212 35 77 10142 5 35 12 77 10142 35 5 12 77 101

18 Reducing the Number of Comparisons On the N th “ bubble up ”, we only need to do MAX-N comparisons. For example: – This is the 4 th “ bubble up ” – MAX is 6 – Thus we have 2 comparisons to do 42 5 35 12 77 101

19 Putting It All Together

20 begin to_do, index to_do <- N – 1 while not (to_do = 0) index <- 1 while not (index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) index <- index + 1 to_do <- to_do – 1 end

21 An Animated Example 674523146339842 1 2 3 4 5 6 7 8 to_do index 7 N 8 did_swap true

22 An Animated Example 674523146339842 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 did_swap false

23 An Animated Example 674523146339842 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap false

24 An Animated Example 674598146332342 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap true

25 An Animated Example 674598146332342 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 did_swap true

26 An Animated Example 674598146332342 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true

27 An Animated Example 679845146332342 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true

28 An Animated Example 679845146332342 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 did_swap true

29 An Animated Example 679845146332342 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true

30 An Animated Example 671445986332342 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true

31 An Animated Example 671445986332342 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 did_swap true

32 An Animated Example 671445986332342 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true

33 An Animated Example 671445698332342 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true

34 An Animated Example 671445698332342 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 did_swap true

35 An Animated Example 671445698332342 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true

36 An Animated Example 981445667332342 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true

37 An Animated Example 981445667332342 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 did_swap true

38 An Animated Example 981445667332342 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true

39 An Animated Example 331445667982342 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true

40 An Animated Example 331445667982342 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 did_swap true

41 An Animated Example 331445667982342 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 Swap did_swap true

42 An Animated Example 331445667422398 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 Swap did_swap true

43 After First Pass of Outer Loop 331445667422398 1 2 3 4 5 6 7 8 to_do index 7 8 N 8 Finished first “ Bubble Up ” did_swap true

44 The Second “ Bubble Up ” 331445667422398 1 2 3 4 5 6 7 8 to_do index 6 1 N 8 did_swap false

45 The Second “ Bubble Up ” 331445667422398 1 2 3 4 5 6 7 8 to_do index 6 1 N 8 did_swap false No Swap

46 The Second “ Bubble Up ” 331445667422398 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap false

47 The Second “ Bubble Up ” 331445667422398 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap false Swap

48 The Second “ Bubble Up ” 334514667422398 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap true Swap

49 The Second “ Bubble Up ” 334514667422398 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true

50 The Second “ Bubble Up ” 334514667422398 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true Swap

51 The Second “ Bubble Up ” 336144567422398 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true Swap

52 The Second “ Bubble Up ” 336144567422398 1 2 3 4 5 6 7 8 to_do index 6 4 N 8 did_swap true

53 The Second “ Bubble Up ” 336144567422398 1 2 3 4 5 6 7 8 to_do index 6 4 N 8 did_swap true No Swap

54 The Second “ Bubble Up ” 336144567422398 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true

55 The Second “ Bubble Up ” 336144567422398 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true Swap

56 The Second “ Bubble Up ” 676144533422398 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true Swap

57 The Second “ Bubble Up ” 676144533422398 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true

58 The Second “ Bubble Up ” 676144533422398 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true Swap

59 The Second “ Bubble Up ” 426144533672398 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true Swap

60 After Second Pass of Outer Loop 426144533672398 1 2 3 4 5 6 7 8 to_do index 6 7 N 8 did_swap true Finished second “ Bubble Up ”

61 The Third “ Bubble Up ” 426144533672398 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap false

62 The Third “ Bubble Up ” 426144533672398 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap false Swap

63 The Third “ Bubble Up ” 426234533671498 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap true Swap

64 The Third “ Bubble Up ” 426234533671498 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true

65 The Third “ Bubble Up ” 426234533671498 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true Swap

66 The Third “ Bubble Up ” 422364533671498 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true Swap

67 The Third “ Bubble Up ” 422364533671498 1 2 3 4 5 6 7 8 to_do index 5 3 N 8 did_swap true

68 The Third “ Bubble Up ” 422364533671498 1 2 3 4 5 6 7 8 to_do index 5 3 N 8 did_swap true No Swap

69 The Third “ Bubble Up ” 422364533671498 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true

70 The Third “ Bubble Up ” 422364533671498 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true Swap

71 The Third “ Bubble Up ” 422363345671498 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true Swap

72 The Third “ Bubble Up ” 422363345671498 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true

73 The Third “ Bubble Up ” 422363345671498 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true Swap

74 The Third “ Bubble Up ” 452363342671498 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true Swap

75 After Third Pass of Outer Loop 452363342671498 1 2 3 4 5 6 7 8 to_do index 5 6 N 8 did_swap true Finished third “ Bubble Up ”

76 The Fourth “ Bubble Up ” 452363342671498 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap false

77 The Fourth “ Bubble Up ” 452363342671498 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap false Swap

78 The Fourth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap true Swap

79 The Fourth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 4 2 N 8 did_swap true

80 The Fourth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 4 2 N 8 did_swap true No Swap

81 The Fourth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 4 3 N 8 did_swap true

82 The Fourth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 4 3 N 8 did_swap true No Swap

83 The Fourth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 4 4 N 8 did_swap true

84 The Fourth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 4 4 N 8 did_swap true No Swap

85 After Fourth Pass of Outer Loop 452314334267698 1 2 3 4 5 6 7 8 to_do index 4 5 N 8 did_swap true Finished fourth “ Bubble Up ”

86 The Fifth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 3 1 N 8 did_swap false

87 The Fifth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 3 1 N 8 did_swap false No Swap

88 The Fifth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 3 2 N 8 did_swap false

89 The Fifth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 3 2 N 8 did_swap false No Swap

90 The Fifth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 3 3 N 8 did_swap false

91 The Fifth “ Bubble Up ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 3 3 N 8 did_swap false No Swap

92 After Fifth Pass of Outer Loop 452314334267698 1 2 3 4 5 6 7 8 to_do index 3 4 N 8 did_swap false Finished fifth “ Bubble Up ”

93 Finished “ Early ” 452314334267698 1 2 3 4 5 6 7 8 to_do index 3 4 N 8 did_swap false We didn ’ t do any swapping, so all of the other elements must be correctly placed. We can “ skip ” the last two passes of the outer loop.

94 Summary “ Bubble Up ” algorithm will move largest value to its correct location (to the right) Repeat “ Bubble Up ” until all elements are correctly placed: – Maximum of N-1 times – Can finish early if no swapping occurs We reduce the number of elements we compare each time one is correctly placed

95 Truth in CS Act NOBODY EVER USES BUBBLE SORT BECAUSE IT IS EXTREMELY INEFFICIENT

96 Mergesort

97 Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 5 12 35 42 77101

98 Divide and Conquer Divide and Conquer cuts the problem in half each time, but uses the result of both halves: – cut the problem in half until the problem is trivial – solve for both halves – combine the solutions

99 Mergesort A divide-and-conquer algorithm: Divide the unsorted array into 2 halves until the sub-arrays only contain one element Merge the sub-problem solutions together: – Compare the sub-array ' s first elements – Remove the smallest element and put it into the result array – Continue the process until all elements have been put into the result array

100 Algorithm Mergesort(Passed an array) if array size > 1 Divide array in half Call Mergesort on first half. Call Mergesort on second half. Merge two halves. Merge(Passed two arrays) Compare leading element in each array Select lower and place in new array. (If one input array is empty then place remainder of other array in output array)

101 674523146339842

102 674523146339842 674523146339842

103 674523146339842 674523146339842 45231498

104 674523146339842 674523146339842 45231498 2398

105 674523146339842 674523146339842 45231498 2398 Merge

106 674523146339842 674523146339842 45231498 2398 23 Merge

107 674523146339842 674523146339842 45231498 2398 2398 Merge

108 674523146339842 674523146339842 45231498 23984514 2398

109 674523146339842 674523146339842 45231498 23984514 Merge 2398

110 674523146339842 674523146339842 45231498 23984514 Merge 2398

111 674523146339842 674523146339842 45231498 23984514 45 Merge 239814

112 674523146339842 674523146339842 45231498 23984514 Merge 98451423

113 674523146339842 674523146339842 45231498 23984514 Merge 9814 2345

114 674523146339842 674523146339842 45231498 23984514 Merge 2314 23 9845

115 674523146339842 674523146339842 45231498 23984514 Merge 23984514 2345

116 674523146339842 674523146339842 45231498 23984514 Merge 23984514 234598

117 674523146339842 674523146339842 45231498 23984514 6763342 23984514 234598

118 674523146339842 674523146339842 45231498 23984514 6763342 676 23984514 234598

119 674523146339842 674523146339842 45231498 23984514 6763342 676 Merge 23984514 234598

120 674523146339842 674523146339842 45231498 23984514 6763342 676 6 Merge 23984514 234598

121 674523146339842 674523146339842 45231498 23984514 6763342 676 Merge 239845146 234598

122 674523146339842 674523146339842 45231498 23984514 6763342 6763342 23984514676 14234598

123 674523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 23984514676 14234598

124 674523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 3323984514676 14234598

125 674523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 422398451467633 14234598

126 674523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 239845146764233 14234598

127 674523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 2398451464233 14234598 6 67

128 4523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 23984514633 14234598 633 6742

129 674523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 2398451464233 14234598 63342 67

130 4523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 239845146764233 14234598 6334267

131 4523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 239845146764233 234598 334267 14 6

132 674523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 239845146764233 234598 64267 6 14 33

133 674523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 239845146764233 144598 64267 614 23 33

134 674523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 239845146764233 142398 64267 61423 45 33

135 674523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 239845146764233 142398 63367 6142333 45 42

136 674523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 239845146764233 142398 63342 614233342 45 67

137 4523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 239845146764233 142345 63342 61423334245 98 67

138 4523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 239845146764233 14234598 6334267 6142333424567

139 4523146339842 674523146339842 45231498 23984514 6763342 6763342 Merge 239845146764233 14234598 6334267 614233342456798

140 674523146339842 674523146339842 45231498 23984514 6763342 6763342 239845146764233 14234598 6334267 614233342456798

141 674523146339842 614233342456798

142 Summary Divide the unsorted collection into two Until the sub-arrays only contain one element Then merge the sub-problem solutions together

143 Summary: what you now know Some examples of sorting using algorithms. Some issues about how long they might be. Some intuitions about correctness Thanks to Leahy and Smith (2000) 143 CS1022


Download ppt "CS1022 Computer Programming & Principles Lecture 2.2 Algorithms."

Similar presentations


Ads by Google