Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort...
Transcript of Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort...
![Page 1: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/1.jpg)
Bubble Sort
With thanks to Bill Leahy, GaTech for original slide deck
![Page 2: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/2.jpg)
Sorting
• Sorting takes an unordered collection and makes it an ordered one.
512354277 101
0 1 2 3 4 5
5 12 35 42 77 101
0 1 2 3 4 5
![Page 3: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/3.jpg)
Sorting
• Lots of algorithms for sorting
– Selection sort (video lecture and textbook)
– Insertion sort (textbook)– Bubble sort– Merge sort – Radix sort
– ...
![Page 4: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/4.jpg)
"Bubbling Up" the Largest Element
• Traverse a collection of elements (array)
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise comparisons and swapping
512354277 101
0 1 2 3 4 5
![Page 5: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/5.jpg)
"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
512354277 101Swap42 77
0 1 2 3 4 5
![Page 6: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/6.jpg)
"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
512357742 101Swap35 77
0 1 2 3 4 5
![Page 7: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/7.jpg)
"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
512773542 101Swap12 77
0 1 2 3 4 5
![Page 8: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/8.jpg)
"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
577123542 101
No need to swap
0 1 2 3 4 5
![Page 9: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/9.jpg)
"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
577123542 101Swap5 101
0 1 2 3 4 5
![Page 10: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/10.jpg)
"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
77123542 5 101
Largest value correctly placed
0 1 2 3 4 5
![Page 11: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/11.jpg)
The “Bubble Up” Algorithm
Given some array, a
for(int i = 0; i < a.length-1; i++) { if(a[i] > a[i+1]) {
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp; }}
![Page 12: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/12.jpg)
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
77123542 5 101
Largest value correctly placed
0 1 2 3 4 5
![Page 13: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/13.jpg)
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.
![Page 14: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/14.jpg)
“Bubbling” All the Elements
77123542 5 101
5421235 770 1 2 3 4 5
101
42 5 3512 770 1 2 3 4 5
101
42 35 512 770 1 2 3 4 5
101
42 35 12 5 770 1 2 3 4 5
101
N -
1
0 1 2 3 4 5
![Page 15: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/15.jpg)
Reducing the Number of Comparisons
12 35 42 77 1010 1 2 3 4 5
5
77123542 50 1 2 3 4 5
101
5421235 770 1 2 3 4 5
101
42 5 3512 770 1 2 3 4 5
101
42 35 512 770 1 2 3 4 5
101
![Page 16: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/16.jpg)
Reducing the Number of Comparisons
• On the Nth “bubble up”, we only need to do MAX-N comparisons.
• For example:
– This is the 4th “bubble up”
– MAX is 6 (total number of elements in array)
– Thus we have 2 comparisons to do
42 5 3512 770 1 2 3 4 5
101
![Page 17: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/17.jpg)
Putting It All Together
![Page 18: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/18.jpg)
public static void Bubblesort(int[] a){ int to_do = a.length-1;
while (to_do != 0) { for(int i = 0; i < to_do; i++) { if(a[i] > a[i+1]) { int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp; } } to_do = to_do – 1; } // end while loop} //end method
Inn
er l
oo
p
Ou
ter
loo
p
![Page 19: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/19.jpg)
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 770 1 2 3 4 5
101
![Page 20: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/20.jpg)
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.”
![Page 21: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/21.jpg)
public static void Bubblesort(int[] a){ int to_do = a.length-1; boolean did_swap = true;
while (to_do != 0 && did_swap) { did_swap = false; for(int i = 0; i < to_do; i++) { if(a[i] > a[i+1]) { did_swap = true; int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp; } } to_do = to_do – 1; } // end while} //end method
![Page 22: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/22.jpg)
An Animated Example
674523 14 6 3398 42
to_do
i
7
0
did_swap false
0 1 2 3 4 5 6 7
![Page 23: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/23.jpg)
An Animated Example
674523 14 6 3398 42
to_do
i
7
0
Swap
did_swap false
0 1 2 3 4 5 6 7
![Page 24: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/24.jpg)
An Animated Example
674598 14 6 3323 42
to_do
i
7
1
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 25: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/25.jpg)
An Animated Example
674598 14 6 3323 42
to_do
i
7
1
did_swap true
0 1 2 3 4 5 6 7
![Page 26: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/26.jpg)
An Animated Example
674598 14 6 3323 42
to_do
i
7
1
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 27: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/27.jpg)
An Animated Example
679845 14 6 3323 42
to_do
i
7
1
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 28: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/28.jpg)
An Animated Example
679845 14 6 3323 42
to_do
i
7
2
did_swap true
0 1 2 3 4 5 6 7
![Page 29: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/29.jpg)
An Animated Example
679845 14 6 3323 42
to_do
i
7
2
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 30: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/30.jpg)
An Animated Example
671445 98 6 3323 42
to_do
i
7
2
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 31: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/31.jpg)
An Animated Example
671445 98 6 3323 42
to_do
i
7
3
did_swap true
0 1 2 3 4 5 6 7
![Page 32: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/32.jpg)
An Animated Example
671445 98 6 3323 42
to_do
i
7
3
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 33: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/33.jpg)
An Animated Example
671445 6 98 3323 42
to_do
i
7
3
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 34: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/34.jpg)
An Animated Example
671445 6 98 3323 42
to_do
i
7
4
did_swap true
0 1 2 3 4 5 6 7
![Page 35: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/35.jpg)
An Animated Example
671445 6 98 3323 42
to_do
i
7
4
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 36: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/36.jpg)
An Animated Example
981445 6 67 3323 42
to_do
i
7
4
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 37: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/37.jpg)
An Animated Example
981445 6 67 3323 42
to_do
i
7
5
did_swap true
0 1 2 3 4 5 6 7
![Page 38: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/38.jpg)
An Animated Example
981445 6 67 3323 42
to_do
i
7
5
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 39: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/39.jpg)
An Animated Example
331445 6 67 9823 42
to_do
i
7
5
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 40: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/40.jpg)
An Animated Example
331445 6 67 9823 42
to_do
i
7
6
did_swap true
0 1 2 3 4 5 6 7
![Page 41: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/41.jpg)
An Animated Example
331445 6 67 9823 42
to_do
i
7
6
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 42: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/42.jpg)
An Animated Example
331445 6 67 4223 98
to_do
i
7
6
Swap
did_swap true
0 1 2 3 4 5 6 7
![Page 43: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/43.jpg)
After First Pass of Outer Loop
331445 6 67 4223 98
to_do
i
7
7 Finished first “Bubble Up”
did_swap true
0 1 2 3 4 5 6 7
![Page 44: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/44.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
to_do
i
6
0
did_swap false
0 1 2 3 4 5 6 7
![Page 45: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/45.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
to_do
i
6
0
did_swap false
No Swap
0 1 2 3 4 5 6 7
![Page 46: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/46.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
to_do
i
6
1
did_swap false
0 1 2 3 4 5 6 7
![Page 47: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/47.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
to_do
i
6
1
did_swap false
Swap
0 1 2 3 4 5 6 7
![Page 48: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/48.jpg)
The Second “Bubble Up”
334514 6 67 4223 98
to_do
i
6
1
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 49: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/49.jpg)
The Second “Bubble Up”
334514 6 67 4223 98
to_do
i
6
2
did_swap true
0 1 2 3 4 5 6 7
![Page 50: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/50.jpg)
The Second “Bubble Up”
334514 6 67 4223 98
to_do
i
6
2
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 51: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/51.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
0 1 2 3 4 5 6 7
to_do
i
6
2
did_swap true
Swap
![Page 52: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/52.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
to_do
i
6
3
did_swap true
0 1 2 3 4 5 6 7
![Page 53: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/53.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
to_do
i
6
3
did_swap true
No Swap
0 1 2 3 4 5 6 7
![Page 54: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/54.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
to_do
i
6
4
did_swap true
0 1 2 3 4 5 6 7
![Page 55: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/55.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
to_do
i
6
4
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 56: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/56.jpg)
The Second “Bubble Up”
67614 45 33 4223 98
to_do
i
6
4
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 57: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/57.jpg)
The Second “Bubble Up”
67614 45 33 4223 98
to_do
i
6
5
did_swap true
0 1 2 3 4 5 6 7
![Page 58: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/58.jpg)
The Second “Bubble Up”
67614 45 33 4223 98
to_do
i
6
5
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 59: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/59.jpg)
The Second “Bubble Up”
42614 45 33 6723 98
to_do
i
6
5
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 60: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/60.jpg)
After Second Pass of Outer Loop
42614 45 33 6723 98
to_do
i
6
6
did_swap true
Finished second “Bubble Up”
0 1 2 3 4 5 6 7
![Page 61: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/61.jpg)
The Third “Bubble Up”
42614 45 33 6723 98
to_do
i
5
0
did_swap false
0 1 2 3 4 5 6 7
![Page 62: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/62.jpg)
The Third “Bubble Up”
42614 45 33 6723 98
to_do
i
5
0
did_swap false
Swap
0 1 2 3 4 5 6 7
![Page 63: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/63.jpg)
The Third “Bubble Up”
42623 45 33 6714 98
to_do
i
5
0
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 64: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/64.jpg)
The Third “Bubble Up”
42623 45 33 6714 98
to_do
i
5
1
did_swap true
0 1 2 3 4 5 6 7
![Page 65: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/65.jpg)
The Third “Bubble Up”
42623 45 33 6714 98
to_do
i
5
1
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 66: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/66.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
to_do
i
5
1
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 67: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/67.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
to_do
i
5
2
did_swap true
0 1 2 3 4 5 6 7
![Page 68: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/68.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
to_do
i
5
2
did_swap true
No Swap
0 1 2 3 4 5 6 7
![Page 69: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/69.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
to_do
i
5
3
did_swap true
0 1 2 3 4 5 6 7
![Page 70: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/70.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
to_do
i
5
3
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 71: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/71.jpg)
The Third “Bubble Up”
42236 33 45 6714 98
to_do
i
5
3
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 72: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/72.jpg)
The Third “Bubble Up”
42236 33 45 6714 98
to_do
i
5
4
did_swap true
0 1 2 3 4 5 6 7
![Page 73: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/73.jpg)
The Third “Bubble Up”
42236 33 45 6714 98
to_do
i
5
4
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 74: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/74.jpg)
The Third “Bubble Up”
45236 33 42 6714 98
to_do
i
5
4
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 75: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/75.jpg)
After Third Pass of Outer Loop
45236 33 42 6714 98
to_do
i
5
5
did_swap true
Finished third “Bubble Up”
0 1 2 3 4 5 6 7
![Page 76: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/76.jpg)
The Fourth “Bubble Up”
45236 33 42 6714 98
to_do
i
4
0
did_swap false
0 1 2 3 4 5 6 7
![Page 77: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/77.jpg)
The Fourth “Bubble Up”
45236 33 42 6714 98
to_do
i
4
0
did_swap false
Swap
0 1 2 3 4 5 6 7
![Page 78: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/78.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
to_do
i
4
0
did_swap true
Swap
0 1 2 3 4 5 6 7
![Page 79: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/79.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
to_do
i
4
1
did_swap true
0 1 2 3 4 5 6 7
![Page 80: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/80.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
to_do
i
4
1
did_swap true
No Swap
0 1 2 3 4 5 6 7
![Page 81: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/81.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
to_do
i
4
2
did_swap true
0 1 2 3 4 5 6 7
![Page 82: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/82.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
to_do
i
4
2
did_swap true
No Swap
0 1 2 3 4 5 6 7
![Page 83: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/83.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
to_do
i
4
3
did_swap true
0 1 2 3 4 5 6 7
![Page 84: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/84.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
to_do
i
4
3
did_swap true
No Swap
0 1 2 3 4 5 6 7
![Page 85: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/85.jpg)
After Fourth Pass of Outer Loop
452314 33 42 676 98
to_do
i
4
4
did_swap true
Finished fourth “Bubble Up”
0 1 2 3 4 5 6 7
![Page 86: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/86.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
to_do
i
3
0
did_swap false
0 1 2 3 4 5 6 7
![Page 87: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/87.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
to_do
i
3
0
did_swap false
No Swap
0 1 2 3 4 5 6 7
![Page 88: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/88.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
to_do
i
3
1
did_swap false
0 1 2 3 4 5 6 7
![Page 89: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/89.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
to_do
i
3
1
did_swap false
No Swap
0 1 2 3 4 5 6 7
![Page 90: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/90.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
to_do
i
3
2
did_swap false
0 1 2 3 4 5 6 7
![Page 91: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/91.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
to_do
i
3
2
did_swap false
No Swap
0 1 2 3 4 5 6 7
![Page 92: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/92.jpg)
After Fifth Pass of Outer Loop
452314 33 42 676 98
to_do
i
3
3
did_swap false
Finished fifth “Bubble Up”
0 1 2 3 4 5 6 7
![Page 93: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/93.jpg)
Finished “Early”
452314 33 42 676 98
to_do
i
3
3
did_swap falseWe didn’t do any swapping,so all of the other elementsmust be correctly placed.
We can “skip” the last twopasses of the outer loop.
0 1 2 3 4 5 6 7
![Page 94: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/94.jpg)
Summary
• “Bubble Up” algorithm will move largest value to its correct location (to the right/end of array)
• 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
![Page 95: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/95.jpg)
Truth in CS Act
• NOBODY EVER USES BUBBLE SORT
• NOBODY
• NOT EVER
• BECAUSE IT IS EXTREMELY INEFFICIENT
![Page 96: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge](https://reader031.fdocuments.net/reader031/viewer/2022021909/5bf5940409d3f237308bf953/html5/thumbnails/96.jpg)
Questions?