# Sorting Example Bubble Sort

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

Sorting Example Bubble Sort

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:

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

"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

"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

"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

"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

"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

"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

"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

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

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

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.

“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

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

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

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

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

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

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

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

Code Demo

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Sorting Algorithms Courtesy of

Similar presentations