What is Bubble Sort Sorting 8 Sorting takes
What is Bubble Sort
Sorting 8 Sorting takes an unordered collection and makes it an ordered one. 1 77 2 42 1 2 5 12 3 35 4 35 101 12 3 5 4 42 6 5 5 77 6 101
EXAMPLE 1 CS 307 Fundamentals of Computer Science 3
"Bubbling Up" the Largest Element 8 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 1 77 2 42 3 35 4 12 5 101 6 5
"Bubbling Up" the Largest Element 8 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 1 2 42 Swap 77 42 77 3 35 4 12 5 101 6 5
"Bubbling Up" the Largest Element 8 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 1 42 2 3 35 Swap 77 77 35 4 12 5 101 6 5
"Bubbling Up" the Largest Element 8 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 1 42 2 35 3 4 5 12 Swap 12 77 101 77 5 6
"Bubbling Up" the Largest Element 8 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 1 42 2 35 3 12 4 77 5 101 No need to swap 6 5
"Bubbling Up" the Largest Element 8 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 1 42 2 35 3 12 4 77 5 5 Swap 101 5 6
"Bubbling Up" the Largest Element 8 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 1 42 2 35 3 12 4 77 5 5 Largest value correctly placed 6 101
Items of Interest 8 Notice that only the largest value is correctly placed 8 All other values are still out of order 8 So we need to repeat this process 1 42 2 35 3 12 4 77 5 5 Largest value correctly placed 6 101
Repeat “Bubble Up” How Many Times? 8 If we have N elements… 8 And if each time we bubble an element, we place it in its correct location… 8 Then we repeat the “bubble up” process N – 1 times. 8 This guarantees we’ll correctly place all N elements.
N-1 “Bubbling” All the Elements 1 42 1 35 1 12 1 5 2 35 2 12 3 4 77 12 3 5 42 3 5 3 35 5 5 4 101 77 42 6 5 4 6 101 5 77 4 42 101 77 42 6 6 101 5 77 6 101
EXAMPLE 2 CS 307 Fundamentals of Computer Science 14
An Animated Example N 8 to_do 7 index did_swap true 98 23 45 14 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 1 did_swapfalse 98 23 45 14 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 1 did_swapfalse Swap 98 23 45 14 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 1 did_swap true Swap 23 98 45 14 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 2 did_swap true 23 98 45 14 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 2 did_swap true Swap 23 98 45 14 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 2 did_swap true Swap 23 45 98 14 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 3 did_swap true 23 45 98 14 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 3 did_swap true Swap 23 45 98 14 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 3 did_swap true Swap 23 45 14 98 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 4 did_swap true 23 45 14 98 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 4 did_swap true Swap 23 45 14 98 6 67 33 42 1 2 3 4 5 6 7 8
An Animated Example N 8 to_do 7 index 4 did_swap true Swap 23 45 14 6 1 2 3 98 67 33 42 4 5 6 7 8
An Animated Example N 8 to_do 7 index 5 did_swap true 23 45 14 6 1 2 3 98 67 33 42 4 5 6 7 8
An Animated Example N 8 to_do 7 index 5 did_swap true Swap 23 45 14 6 1 2 3 98 67 33 42 4 5 6 7 8
An Animated Example N 8 to_do 7 index 5 did_swap true Swap 23 45 14 6 1 2 3 67 98 33 42 4 5 6 7 8
An Animated Example N 8 to_do 7 index 6 did_swap true 23 45 14 6 1 2 3 67 98 33 42 4 5 6 7 8
An Animated Example N 8 to_do 7 index 6 did_swap true Swap 23 45 14 6 1 2 3 67 98 33 42 4 5 6 7 8
An Animated Example N 8 to_do 7 index 6 did_swap true Swap 23 45 14 6 1 2 3 67 33 98 42 4 5 6 7 8
An Animated Example N 8 to_do 7 index 7 did_swap true 23 45 14 6 1 2 3 67 33 98 42 4 5 6 7 8
An Animated Example N 8 to_do 7 index 7 did_swap true Swap 23 45 14 6 1 2 3 67 33 98 42 4 5 6 7 8
An Animated Example N 8 to_do 7 index 7 did_swap true Swap 23 45 14 6 1 2 3 67 33 42 98 4 5 6 7 8
After First Pass of Outer Loop N 8 to_do 7 index 8 did_swap true Finished first “Bubble Up” 23 45 14 6 1 2 3 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 1 did_swapfalse 23 45 14 6 1 2 3 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 1 did_swapfalse No Swap 23 45 14 6 1 2 3 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 2 did_swapfalse 23 45 14 6 1 2 3 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 2 did_swapfalse Swap 23 45 14 6 1 2 3 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 2 did_swap true Swap 23 14 45 6 1 2 3 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 3 did_swap true 23 14 45 6 1 2 3 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 3 did_swap true Swap 23 14 45 6 1 2 3 67 33 42 98 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 3 did_swap true Swap 23 14 6 1 2 45 67 33 42 98 3 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 4 did_swap true 23 14 6 1 2 45 67 33 42 98 3 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 4 did_swap true No Swap 23 14 6 1 2 45 67 33 42 98 3 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 5 did_swap true 23 14 6 1 2 45 67 33 42 98 3 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 5 did_swap true Swap 23 14 6 1 2 45 67 33 42 98 3 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 5 did_swap true Swap 23 14 6 1 2 45 33 67 42 98 3 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 6 did_swap true 23 14 6 1 2 45 33 67 42 98 3 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 6 did_swap true Swap 23 14 6 1 2 45 33 67 42 98 3 4 5 6 7 8
The Second “Bubble Up” N 8 to_do 6 index 6 did_swap true Swap 23 14 6 1 2 45 33 42 67 98 3 4 5 6 7 8
After Second Pass of Outer Loop N 8 to_do 6 index 7 did_swap true Finished second “Bubble Up” 23 14 6 1 2 45 33 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 1 did_swapfalse 23 14 6 1 2 45 33 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 1 did_swapfalse Swap 23 14 6 1 2 45 33 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 1 did_swap true Swap 14 23 6 1 2 45 33 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 2 did_swap true 14 23 6 1 2 45 33 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 2 did_swap true Swap 14 23 6 1 2 45 33 42 67 98 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 2 did_swap true Swap 14 6 1 23 45 33 42 67 98 2 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 3 did_swap true 14 6 1 23 45 33 42 67 98 2 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 3 did_swap true No Swap 14 6 1 23 45 33 42 67 98 2 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 4 did_swap true 14 6 1 23 45 33 42 67 98 2 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 4 did_swap true Swap 14 6 1 23 45 33 42 67 98 2 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 4 did_swap true Swap 14 6 1 23 33 45 42 67 98 2 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 5 did_swap true 14 6 1 23 33 45 42 67 98 2 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 5 did_swap true Swap 14 6 1 23 33 45 42 67 98 2 3 4 5 6 7 8
The Third “Bubble Up” N 8 to_do 5 index 5 did_swap true Swap 14 6 1 23 33 42 45 67 98 2 3 4 5 6 7 8
After Third Pass of Outer Loop N 8 to_do 5 index 6 did_swap true Finished third “Bubble Up” 14 6 1 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swapfalse 14 6 1 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swapfalse Swap 14 6 1 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap true Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 2 6 1 did_swap true 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 2 did_swap true No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 3 6 1 did_swap true 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 3 did_swap true No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 4 6 1 did_swap true 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fourth “Bubble Up” N 8 to_do 4 index 4 did_swap true No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
After Fourth Pass of Outer Loop N 8 to_do 4 index 5 6 1 did_swap true Finished fourth “Bubble Up” 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 1 did_swapfalse 6 14 23 33 42 45 67 98 1 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 1 did_swapfalse No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 2 did_swapfalse 6 14 23 33 42 45 67 98 1 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 2 did_swapfalse No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 3 did_swapfalse 6 14 23 33 42 45 67 98 1 2 3 4 5 6 7 8
The Fifth “Bubble Up” N 8 to_do 3 index 3 did_swapfalse No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
After Fifth Pass of Outer Loop N 8 to_do 3 index 4 6 1 did_swapfalse Finished fifth “Bubble Up” 14 23 33 42 45 67 98 2 3 4 5 6 7 8
Finished “Early” N 8 to_do 3 index 4 6 1 did_swapfalse 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. 14 23 33 42 45 67 98 2 3 4 5 6 7 8
EXAMPLE 3 CS 307 Fundamentals of Computer Science 88
Bubble Sort 12 6 22 14 8 8 17 22 6 12 14 8 17 22 8 Given n numbers to sort: 8 Repeat the following n-1 times: – For each pair of adjacent numbers: • If the number on the left is greater than the number on the right, swap them.
Bubble Sort 6 8 12 12 8 14 17 22 6 8 12 14 17 22 8 Given n numbers to sort: 8 Repeat the following n-1 times: – For each pair of adjacent numbers: • If the number on the left is greater than the number on the right, swap them.
- Slides: 90