Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Example Bubble Sort

Similar presentations


Presentation on theme: "Sorting Example Bubble Sort"— Presentation transcript:

1 Sorting Example Bubble Sort

2 Problem Definition Sorting takes an unordered collection and makes it an ordered one. Input: 77 42 35 12 101 5 5 12 35 42 77 101 Output:

3 One Idea: Bubble 1 Water Air 2 3 1 2 3 1 2 3 1 3 2

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 77 42 35 12 101 5

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 Swap 42 77 77 42 35 12 101 5

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 Swap 35 77 42 77 35 12 101 5

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 Swap 12 77 42 35 77 12 101 5

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 42 35 12 77 101 5 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 Swap 5 101 42 35 12 77 101 5

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 101 42 35 12 77 5 Largest value correctly placed

11 The “Bubble Up” Algorithm
Bubble-Sort-step1( A ): for k = 1 … (N – 1): if A[k] > A[k+1]: Swap( A, k, k+1 ) Swap( A, x, y ): tmp = A[x] A[x] = A[y] A[y] = tmp Python Code in

12 Need More Iterations Notice that only the largest value is correctly placed All other values are still out of order So we need to repeat this process 101 42 35 12 77 5 Largest value correctly placed

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

14 “Bubbling” All the Elements
77 12 35 42 5 101 N - 1 5 42 12 35 77 101 42 5 35 12 77 101 42 35 5 12 77 101 42 35 12 5 77 101

15 The “Bubble Up” Algorithm
Bubble-Sort( A ): for k = 1 … (N – 1): if A[k] > A[k+1]: Swap( A, k, k+1 ) Python Code in

16 The “Bubble Up” Algorithm
Bubble-Sort( A ): for i = 1 … (N – 1): for k = 1 … (N – 1): if A[k] > A[k+1]: Swap( A, k, k+1 ) Python Code in

17 The “Bubble Up” Algorithm
Bubble-Sort( A ): for i = 1 … (N – 1): for k = 1 … (N – 1): if A[k] > A[k+1]: Swap( A, k, k+1 ) To do N-1 iterations To bubble a value Inner loop Outer loop Python Code in

18 Reducing the Number of Comparisons
12 35 42 77 101 5 77 12 35 42 5 101 5 42 12 35 77 101 42 5 35 12 77 101 42 35 5 12 77 101

19 Reducing the Number of Comparisons
Assume the array size  N On the ith iteration, we only need to do N-i comparisons. For example: N = 6 i = 4 (4th iteration) Thus, we have 2 comparisons to do 42 5 35 12 77 101

20 The “Bubble Up” Algorithm
Bubble-Sort( A ): for i = 1 … (N – 1): for k = 1 … (N – 1): if A[k] > A[k+1]: Swap( A, k, k+1 ) Python Code in

21 The “Bubble Up” Algorithm
Bubble-Sort( A ): for i = 1 … (N – 1): for k = 1 … (N – i): if A[k] > A[k+1]: Swap( A, k, k+1 ) Python Code in

22 Code Demo

23 An Animated Example N 8 i 1 k 98 23 45 14 6 67 33 42

24 An Animated Example N 8 i 1 k 1 98 23 45 14 6 67 33 42

25 An Animated Example N 8 i 1 k 1 Swap 98 23 45 14 6 67 33 42

26 An Animated Example N 8 i 1 k 1 Swap 23 98 45 14 6 67 33 42

27 An Animated Example N 8 i 1 k 2 23 98 45 14 6 67 33 42

28 An Animated Example N 8 i 1 k 2 Swap 23 98 45 14 6 67 33 42

29 An Animated Example N 8 i 1 k 2 Swap 23 45 98 14 6 67 33 42

30 An Animated Example N 8 i 1 k 3 23 45 98 14 6 67 33 42

31 An Animated Example N 8 i 1 k 3 Swap 23 45 98 14 6 67 33 42

32 An Animated Example N 8 i 1 k 3 Swap 23 45 14 98 6 67 33 42

33 An Animated Example N 8 i 1 k 4 23 45 14 98 6 67 33 42

34 An Animated Example N 8 i 1 k 4 Swap 23 45 14 98 6 67 33 42

35 An Animated Example N 8 i 1 k 4 Swap 23 45 14 6 98 67 33 42

36 An Animated Example N 8 i 1 k 5 23 45 14 6 98 67 33 42

37 An Animated Example N 8 i 1 k 5 Swap 23 45 14 6 98 67 33 42

38 An Animated Example N 8 i 1 k 5 Swap 23 45 14 6 67 98 33 42

39 An Animated Example N 8 i 1 k 6 23 45 14 6 67 98 33 42

40 An Animated Example N 8 i 1 k 6 Swap 23 45 14 6 67 98 33 42

41 An Animated Example N 8 i 1 k 6 Swap 23 45 14 6 67 33 98 42

42 An Animated Example N 8 i 1 k 7 23 45 14 6 67 33 98 42

43 An Animated Example N 8 i 1 k 7 Swap 23 45 14 6 67 33 98 42

44 An Animated Example N 8 i 1 k 7 Swap 23 45 14 6 67 33 42 98

45 After First Pass of Outer Loop
N 8 i 1 Finished first “Bubble Up” k 8 23 45 14 6 67 33 42 98

46 The Second “Bubble Up” N 8 i 2 k 1 23 45 14 6 67 33 42 98

47 The Second “Bubble Up” N 8 i 2 k 1 No Swap 23 45 14 6 67 33 42 98

48 The Second “Bubble Up” N 8 i 2 k 2 23 45 14 6 67 33 42 98

49 The Second “Bubble Up” N 8 i 2 k 2 Swap 23 45 14 6 67 33 42 98

50 The Second “Bubble Up” N 8 i 2 k 2 Swap 23 14 45 6 67 33 42 98

51 The Second “Bubble Up” N 8 i 2 k 3 23 14 45 6 67 33 42 98

52 The Second “Bubble Up” N 8 i 2 k 3 Swap 23 14 45 6 67 33 42 98

53 The Second “Bubble Up” N 8 i 2 k 3 Swap 23 14 6 45 67 33 42 98

54 The Second “Bubble Up” N 8 i 2 k 4 23 14 6 45 67 33 42 98

55 The Second “Bubble Up” N 8 i 2 k 4 No Swap 23 14 6 45 67 33 42 98

56 The Second “Bubble Up” N 8 i 2 k 5 23 14 6 45 67 33 42 98

57 The Second “Bubble Up” N 8 i 2 k 5 Swap 23 14 6 45 67 33 42 98

58 The Second “Bubble Up” N 8 i 2 k 5 Swap 23 14 6 45 33 67 42 98

59 The Second “Bubble Up” N 8 i 2 k 6 23 14 6 45 33 67 42 98

60 The Second “Bubble Up” N 8 i 2 k 6 Swap 23 14 6 45 33 67 42 98

61 The Second “Bubble Up” N 8 i 2 k 6 Swap 23 14 6 45 33 42 67 98

62 After Second Pass of Outer Loop
8 i 2 Finished second “Bubble Up” k 7 23 14 6 45 33 42 67 98

63 The Third “Bubble Up” N 8 i 3 k 1 23 14 6 45 33 42 67 98

64 The Third “Bubble Up” N 8 i 3 k 1 Swap 23 14 6 45 33 42 67 98

65 The Third “Bubble Up” N 8 i 3 k 1 Swap 14 23 6 45 33 42 67 98

66 The Third “Bubble Up” N 8 i 3 k 2 14 23 6 45 33 42 67 98

67 The Third “Bubble Up” N 8 i 3 k 2 Swap 14 23 6 45 33 42 67 98

68 The Third “Bubble Up” N 8 i 3 k 2 Swap 14 6 23 45 33 42 67 98

69 The Third “Bubble Up” N 8 i 3 k 3 14 6 23 45 33 42 67 98

70 The Third “Bubble Up” N 8 i 3 k 3 No Swap 14 6 23 45 33 42 67 98

71 The Third “Bubble Up” N 8 i 3 k 4 14 6 23 45 33 42 67 98

72 The Third “Bubble Up” N 8 i 3 k 4 Swap 14 6 23 45 33 42 67 98

73 The Third “Bubble Up” N 8 i 3 k 4 Swap 14 6 23 33 45 42 67 98

74 The Third “Bubble Up” N 8 i 3 k 5 14 6 23 33 45 42 67 98

75 The Third “Bubble Up” N 8 i 3 k 5 Swap 14 6 23 33 45 42 67 98

76 The Third “Bubble Up” N 8 i 3 k 5 Swap 14 6 23 33 42 45 67 98

77 After Third Pass of Outer Loop
N 8 i 3 Finished third “Bubble Up” k 6 14 6 23 33 42 45 67 98

78 The Fourth “Bubble Up” N 8 i 4 k 1 14 6 23 33 42 45 67 98

79 The Fourth “Bubble Up” N 8 i 4 k 1 Swap 14 6 23 33 42 45 67 98

80 The Fourth “Bubble Up” N 8 i 4 k 1 Swap 6 14 23 33 42 45 67 98

81 The Fourth “Bubble Up” N 8 i 4 k 2 6 14 23 33 42 45 67 98

82 The Fourth “Bubble Up” N 8 i 4 k 2 No Swap 6 14 23 33 42 45 67 98

83 The Fourth “Bubble Up” N 8 i 4 k 3 6 14 23 33 42 45 67 98

84 The Fourth “Bubble Up” N 8 i 4 k 3 No Swap 6 14 23 33 42 45 67 98

85 The Fourth “Bubble Up” N 8 i 4 k 4 6 14 23 33 42 45 67 98

86 The Fourth “Bubble Up” N 8 i 4 k 4 No Swap 6 14 23 33 42 45 67 98

87 After Fourth Pass of Outer Loop
N 8 i 4 Finished fourth “Bubble Up” k 5 6 14 23 33 42 45 67 98

88 The Fifth “Bubble Up” N 8 i 5 k 1 6 14 23 33 42 45 67 98

89 The Fifth “Bubble Up” N 8 i 5 k 1 No Swap 6 14 23 33 42 45 67 98

90 The Fifth “Bubble Up” N 8 i 5 k 2 6 14 23 33 42 45 67 98

91 The Fifth “Bubble Up” N 8 i 5 k 2 No Swap 6 14 23 33 42 45 67 98

92 The Fifth “Bubble Up” N 8 i 5 k 3 6 14 23 33 42 45 67 98

93 The Fifth “Bubble Up” N 8 i 5 k 3 No Swap 6 14 23 33 42 45 67 98

94 After Fifth Pass of Outer Loop
N 8 i 5 Finished fifth “Bubble Up” k 4 6 14 23 33 42 45 67 98

95 N 8 i 5 k 4 6 14 23 33 42 45 67 98

96 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

97 Truth in CS Act Bubble sort will do the job, but not efficiently
LB Truth in CS Act Bubble sort will do the job, but not efficiently There are more efficient algorithms for sorting

98 Sorting Algorithms Courtesy of


Download ppt "Sorting Example Bubble Sort"

Similar presentations


Ads by Google