Bubble sort
description
Transcript of Bubble sort
![Page 1: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/1.jpg)
Bubble Sort
Prepared By:
Rao Muhammad Salman MCS
The Islamia University BahawalPur(Pakistan)
![Page 2: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/2.jpg)
Sorting
• Sorting takes an unordered collection and makes it an ordered one.
1 2 3 4 5 6 7 8
674523 14 6 3398 42
1 2 3 4 5 6 7 8
452314 33 42 676 98
![Page 3: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/3.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
1 2 3 4 5 6 7 8
674523 14 6 3398 42
![Page 4: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/4.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
Swap
1 2 3 4 5 6 7 8
674598 14 6 3323 42
Value swapped
![Page 5: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/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
Swap
1 2 3 4 5 6 7 8
679845 14 6 3323 42
Value swapped
![Page 6: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/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
Swap
1 2 3 4 5 6 7 8
671445 98 6 3323 42
Value swapped
![Page 7: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/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
Value swapped
Swap
1 2 3 4 5 6 7 8
671445 6 98 3323 42
![Page 8: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/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
Swap
1 2 3 4 5 6 7 8
981445 6 67 3323 42
Value swapped
![Page 9: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/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
Swap
1 2 3 4 5 6 7 8
331445 6 67 9823 42
Value swapped
![Page 10: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/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
Swap
1 2 3 4 5 6 7 8
331445 6 67 4223 98
Value swapped
![Page 11: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/11.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
Largest value correctly placed
1 2 3 4 5 6 7 8
331445 6 67 4223 98
![Page 12: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/12.jpg)
The “Bubble Up” Algorithm
index <- 1last_compare_at <- n – 1
loopouter exitif(index > last_compare_at)Loopinner
to_do<-0 exitif(to_do > index) if(A[to_do] > A[to_do + 1]) then Swap(A[to_do], A[to_do + 1]) endif
to_do<- to_do + 1endloopinner index <- index - 1endloopouter
![Page 13: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/13.jpg)
No, Swap isn’t built in.
Procedure Swap(a, b isoftype in/out Num)
temp isoftype Num
temp <- a
a <- b
b <- temp
endprocedure // Swap
![Page 14: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/14.jpg)
Bubble Sort Code
void bubble_sort( int data[ ], int size)
{
int outer, inner,temp;
for(outer = size-1; outer =>0; --outer)
{
for(inner = 0; inner < outer;++inner)
if(data[inner] > data[inner+1])
{
temp = data[inner];
data[inner] = data[inner+1];
data[inner+1] = temp;
}
}
}
![Page 15: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/15.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
Largest value correctly placed
1 2 3 4 5 6 7 8
331445 6 67 4223 98
![Page 16: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/16.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 largest correct location…
• Then we repeat InnerLoop the “bubble up” process OuterLoop – 1 times.
• This guarantees we’ll correctly place all N elements.
![Page 17: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/17.jpg)
Dry Run
![Page 18: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/18.jpg)
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
N 8 did_swap true
The First “Bubble Up”
![Page 19: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/19.jpg)
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8 did_swap false
The First “Bubble Up”
![Page 20: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/20.jpg)
The First “Bubble Up”
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8
Swap
did_swap false
![Page 21: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/21.jpg)
The First “Bubble Up”
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8
Swap
did_swap true
![Page 22: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/22.jpg)
The First “Bubble Up”
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8 did_swap true
![Page 23: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/23.jpg)
The First “Bubble Up”
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8
Swap
did_swap true
![Page 24: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/24.jpg)
The First “Bubble Up”
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8
Swap
did_swap true
![Page 25: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/25.jpg)
The First “Bubble Up”
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8 did_swap true
![Page 26: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/26.jpg)
The First “Bubble Up”
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8
Swap
did_swap true
![Page 27: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/27.jpg)
The First “Bubble Up”
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8
Swap
did_swap true
![Page 28: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/28.jpg)
The First “Bubble Up”
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8 did_swap true
![Page 29: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/29.jpg)
The First “Bubble Up”
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8
Swap
did_swap true
![Page 30: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/30.jpg)
The First “Bubble Up”
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8
Swap
did_swap true
![Page 31: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/31.jpg)
The First “Bubble Up”
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8 did_swap true
![Page 32: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/32.jpg)
The First “Bubble Up”
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8
Swap
did_swap true
![Page 33: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/33.jpg)
The First “Bubble Up”
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8
Swap
did_swap true
![Page 34: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/34.jpg)
The First “Bubble Up”
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8 did_swap true
![Page 35: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/35.jpg)
The First “Bubble Up”
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8
Swap
did_swap true
![Page 36: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/36.jpg)
The First “Bubble Up”
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8
Swap
did_swap true
![Page 37: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/37.jpg)
The First “Bubble Up”
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8 did_swap true
![Page 38: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/38.jpg)
The First “Bubble Up”
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8
Swap
did_swap true
![Page 39: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/39.jpg)
The First “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8 did_swap true
Finished 1st “Bubble Up”
No Swap
![Page 40: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/40.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
1
N 8 did_swap false
![Page 41: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/41.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
1
N 8 did_swap false
No Swap
![Page 42: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/42.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap false
![Page 43: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/43.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap false
Swap
![Page 44: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/44.jpg)
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap true
Swap
![Page 45: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/45.jpg)
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
![Page 46: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/46.jpg)
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
Swap
![Page 47: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/47.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
Swap
![Page 48: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/48.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
4
N 8 did_swap true
![Page 49: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/49.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
4
N 8 did_swap true
No Swap
![Page 50: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/50.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
![Page 51: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/51.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
Swap
![Page 52: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/52.jpg)
The Second “Bubble Up”
67614 45 33 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
Swap
![Page 53: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/53.jpg)
The Second “Bubble Up”
67614 45 33 4223 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
![Page 54: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/54.jpg)
The Second “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
Swap
![Page 55: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/55.jpg)
The Second “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
Swap
Finished 2nd “Bubble Up”
![Page 56: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/56.jpg)
The Third “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap false
![Page 57: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/57.jpg)
The Third “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap false
Swap
![Page 58: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/58.jpg)
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap true
Swap
![Page 59: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/59.jpg)
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
![Page 60: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/60.jpg)
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
Swap
![Page 61: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/61.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
Swap
![Page 62: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/62.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
3
N 8 did_swap true
![Page 63: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/63.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
3
N 8 did_swap true
No Swap
![Page 64: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/64.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
![Page 65: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/65.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
Swap
![Page 66: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/66.jpg)
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
Swap
![Page 67: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/67.jpg)
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
![Page 68: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/68.jpg)
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
Swap
![Page 69: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/69.jpg)
The Third “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
Swap
Finished 3rd “Bubble Up”
![Page 70: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/70.jpg)
The Fourth “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap false
![Page 71: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/71.jpg)
The Fourth “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap false
Swap
![Page 72: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/72.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap true
Swap
![Page 73: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/73.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
2
N 8 did_swap true
![Page 74: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/74.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
2
N 8 did_swap true
No Swap
![Page 75: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/75.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
3
N 8 did_swap true
![Page 76: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/76.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
3
N 8 did_swap true
No Swap
![Page 77: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/77.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
4
N 8 did_swap true
![Page 78: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/78.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
4
N 8 did_swap true
No Swap
Finished 4th “Bubble Up”
![Page 79: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/79.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
1
N 8 did_swap false
![Page 80: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/80.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
1
N 8 did_swap false
No Swap
![Page 81: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/81.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
2
N 8 did_swap false
![Page 82: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/82.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
2
N 8 did_swap false
No Swap
![Page 83: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/83.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
3
N 8 did_swap false
![Page 84: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/84.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
3
N 8 did_swap false
No Swap
Finished 5th “Bubble Up”
![Page 85: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/85.jpg)
The Sixth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
2
1
N 8 did_swap false
![Page 86: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/86.jpg)
The Sixth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
2
1
N 8 did_swap false
No Swap
![Page 87: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/87.jpg)
The Sixth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
2
2
N 8 did_swap false
![Page 88: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/88.jpg)
The Sixth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
2
2
N 8 did_swap false
No Swap
Finished 6th “Bubble Up”
![Page 89: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/89.jpg)
The Seventh “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
1
1
N 8 did_swap false
![Page 90: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/90.jpg)
The Seventh “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
1
1
N 8 did_swap false
No Swap
Finished 7th “Bubble Up”
![Page 91: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/91.jpg)
Sorted Elements
452314 33 42 676 98
1 2 3 4 5 6 7 8
![Page 92: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/92.jpg)
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
![Page 93: Bubble sort](https://reader034.fdocuments.us/reader034/viewer/2022051514/54981b66b4795960028b45b0/html5/thumbnails/93.jpg)