Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42."— Presentation transcript:

1 Bubble Sort Merge Sort

2 Bubble Sort

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

4 "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

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 Swap 4277

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 3577 42 101 1 2 3 4 5 6 Swap 3577

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 7735 42 101 1 2 3 4 5 6 Swap 1277

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 77 1235 42 101 1 2 3 4 5 6 No need to swap

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 1 2 3 4 5 6 Swap 5101

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 77 1235 42 5 1 2 3 4 5 6 101 Largest value correctly placed

11 The “Bubble Up” Algorithm index <- 1 last_compare_at <- n – 1 loop exitif(index > last_compare_at) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop

12 No, Swap isn’t built in. Procedure Swap(a, b isoftype in/out Num) t isoftype Num t <- a a <- b b <- t endprocedure // Swap LB

13 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

14 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.

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

16 Reducing the Number of Comparisons 12 35 42 77 101 1 2 3 4 5 6 577 1235 42 5 1 2 3 4 5 6 101 5 4212 35 77 1 2 3 4 5 6 10142 5 35 12 77 1 2 3 4 5 6 10142 35 5 12 77 1 2 3 4 5 6 101

17 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 1 2 3 4 5 6 101

18 Putting It All Together

19 N is … // Size of Array Arr_Type definesa Array[1..N] of Num Procedure Swap(n1, n2 isoftype in/out Num) temp isoftype Num temp <- n1 n1 <- n2 n2 <- temp endprocedure // Swap

20 procedure Bubblesort(A isoftype in/out Arr_Type) to_do, index isoftype Num to_do <- N – 1 loop exitif(to_do = 0) index <- 1 loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop to_do <- to_do - 1 endloop endprocedure // Bubblesort Inner loop Outer loop

21 Already Sorted Collections? What if the collection was already sorted? What if only a few elements were out of place and after a couple of “bubble ups,” the collection was sorted? We want to be able to detect this and “stop early”! 42 35 12 5 77 1 2 3 4 5 6 101

22 Using a Boolean “Flag” We can use a boolean variable to determine if any swapping occurred during the “bubble up.” If no swapping occurred, then we know that the collection is already sorted! This boolean “flag” needs to be reset after each “bubble up.”

23 did_swap isoftype Boolean did_swap <- true loop exitif ((to_do = 0) OR NOT(did_swap)) index <- 1 did_swap <- false loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) did_swap <- true endif index <- index + 1 endloop to_do <- to_do - 1 endloop

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

46 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

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

48 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

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

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

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

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

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

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

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

56 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

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

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

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

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

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

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

63 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”

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

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

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

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

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

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

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

71 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

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

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

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

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

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

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

78 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”

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

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

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

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

83 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

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

85 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

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

87 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

88 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”

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

90 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

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

92 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

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

94 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

95 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”

96 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.


Download ppt "Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42."

Similar presentations


Ads by Google