bubble sort
TRANSCRIPT
![Page 1: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/1.jpg)
Bubble Sort
![Page 2: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/2.jpg)
Sorting• Sorting takes an unordered collection and
makes it an ordered one.
512354277 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
![Page 3: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/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
512354277 101
1 2 3 4 5 6
![Page 4: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/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
512354277 101
1 2 3 4 5 6Swap42 77
![Page 5: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/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
512357742 101
1 2 3 4 5 6Swap35 77
![Page 6: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/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
512773542 101
1 2 3 4 5 6Swap12 77
![Page 7: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/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
577123542 101
1 2 3 4 5 6
No need to swap
![Page 8: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/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
1 2 3 4 5 6Swap5 101
![Page 9: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/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
77123542 5
1 2 3 4 5 6
101
Largest value correctly placed
![Page 10: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/10.jpg)
The “Bubble Up” Algorithm
index <- 1last_compare_at <- n – 1
loop exitif(index > last_compare_at) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1endloop
![Page 11: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/11.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
1 2 3 4 5 6
101
Largest value correctly placed
![Page 12: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/12.jpg)
An Animated Example
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_doindex
7
N 8 did_swap true
![Page 13: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/13.jpg)
An Animated Example
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_doindex
71
N 8 did_swap false
![Page 14: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/14.jpg)
An Animated Example
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_doindex
71
N 8
Swap
did_swap false
![Page 15: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/15.jpg)
An Animated Example
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_doindex
71
N 8
Swap
did_swap true
![Page 16: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/16.jpg)
An Animated Example
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_doindex
72
N 8 did_swap true
![Page 17: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/17.jpg)
An Animated Example
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_doindex
72
N 8
Swap
did_swap true
![Page 18: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/18.jpg)
An Animated Example
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_doindex
72
N 8
Swap
did_swap true
![Page 19: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/19.jpg)
An Animated Example
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_doindex
73
N 8 did_swap true
![Page 20: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/20.jpg)
An Animated Example
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_doindex
73
N 8
Swap
did_swap true
![Page 21: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/21.jpg)
An Animated Example
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_doindex
73
N 8
Swap
did_swap true
![Page 22: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/22.jpg)
An Animated Example
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_doindex
74
N 8 did_swap true
![Page 23: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/23.jpg)
An Animated Example
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_doindex
74
N 8
Swap
did_swap true
![Page 24: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/24.jpg)
An Animated Example
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_doindex
74
N 8
Swap
did_swap true
![Page 25: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/25.jpg)
An Animated Example
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_doindex
75
N 8 did_swap true
![Page 26: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/26.jpg)
An Animated Example
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_doindex
75
N 8
Swap
did_swap true
![Page 27: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/27.jpg)
An Animated Example
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_doindex
75
N 8
Swap
did_swap true
![Page 28: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/28.jpg)
An Animated Example
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_doindex
76
N 8 did_swap true
![Page 29: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/29.jpg)
An Animated Example
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_doindex
76
N 8
Swap
did_swap true
![Page 30: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/30.jpg)
An Animated Example
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_doindex
76
N 8
Swap
did_swap true
![Page 31: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/31.jpg)
An Animated Example
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_doindex
77
N 8 did_swap true
![Page 32: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/32.jpg)
An Animated Example
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_doindex
77
N 8
Swap
did_swap true
![Page 33: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/33.jpg)
An Animated Example
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
77
N 8
Swap
did_swap true
![Page 34: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/34.jpg)
After First Pass of Outer Loop
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
78
N 8
Finished first “Bubble Up”
did_swap true
![Page 35: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/35.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
61
N 8 did_swap false
![Page 36: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/36.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
61
N 8 did_swap false
No Swap
![Page 37: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/37.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
62
N 8 did_swap false
![Page 38: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/38.jpg)
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
62
N 8 did_swap false
Swap
![Page 39: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/39.jpg)
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
62
N 8 did_swap true
Swap
![Page 40: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/40.jpg)
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
63
N 8 did_swap true
![Page 41: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/41.jpg)
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
63
N 8 did_swap true
Swap
![Page 42: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/42.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
63
N 8 did_swap true
Swap
![Page 43: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/43.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
64
N 8 did_swap true
![Page 44: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/44.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
64
N 8 did_swap true
No Swap
![Page 45: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/45.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
65
N 8 did_swap true
![Page 46: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/46.jpg)
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
to_doindex
65
N 8 did_swap true
Swap
![Page 47: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/47.jpg)
The Second “Bubble Up”
67614 45 33 4223 98
1 2 3 4 5 6 7 8
to_doindex
65
N 8 did_swap true
Swap
![Page 48: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/48.jpg)
The Second “Bubble Up”
67614 45 33 4223 98
1 2 3 4 5 6 7 8
to_doindex
66
N 8 did_swap true
![Page 49: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/49.jpg)
The Second “Bubble Up”
67614 45 33 4223 98
1 2 3 4 5 6 7 8
to_doindex
66
N 8 did_swap true
Swap
![Page 50: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/50.jpg)
The Second “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_doindex
66
N 8 did_swap true
Swap
![Page 51: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/51.jpg)
After Second Pass of Outer Loop
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_doindex
67
N 8 did_swap true
Finished second “Bubble Up”
![Page 52: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/52.jpg)
The Third “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_doindex
51
N 8 did_swap false
![Page 53: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/53.jpg)
The Third “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_doindex
51
N 8 did_swap false
Swap
![Page 54: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/54.jpg)
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
to_doindex
51
N 8 did_swap true
Swap
![Page 55: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/55.jpg)
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
to_doindex
52
N 8 did_swap true
![Page 56: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/56.jpg)
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
to_doindex
52
N 8 did_swap true
Swap
![Page 57: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/57.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_doindex
52
N 8 did_swap true
Swap
![Page 58: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/58.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_doindex
53
N 8 did_swap true
![Page 59: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/59.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_doindex
53
N 8 did_swap true
No Swap
![Page 60: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/60.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_doindex
54
N 8 did_swap true
![Page 61: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/61.jpg)
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
to_doindex
54
N 8 did_swap true
Swap
![Page 62: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/62.jpg)
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
to_doindex
54
N 8 did_swap true
Swap
![Page 63: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/63.jpg)
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
to_doindex
55
N 8 did_swap true
![Page 64: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/64.jpg)
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
to_doindex
55
N 8 did_swap true
Swap
![Page 65: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/65.jpg)
The Third “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_doindex
55
N 8 did_swap true
Swap
![Page 66: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/66.jpg)
After Third Pass of Outer Loop
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_doindex
56
N 8 did_swap true
Finished third “Bubble Up”
![Page 67: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/67.jpg)
The Fourth “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_doindex
41
N 8 did_swap false
![Page 68: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/68.jpg)
The Fourth “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_doindex
41
N 8 did_swap false
Swap
![Page 69: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/69.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
41
N 8 did_swap true
Swap
![Page 70: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/70.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
42
N 8 did_swap true
![Page 71: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/71.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
42
N 8 did_swap true
No Swap
![Page 72: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/72.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
43
N 8 did_swap true
![Page 73: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/73.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
43
N 8 did_swap true
No Swap
![Page 74: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/74.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
44
N 8 did_swap true
![Page 75: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/75.jpg)
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
44
N 8 did_swap true
No Swap
![Page 76: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/76.jpg)
After Fourth Pass of Outer Loop
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
45
N 8 did_swap true
Finished fourth “Bubble Up”
![Page 77: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/77.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
31
N 8 did_swap false
![Page 78: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/78.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
31
N 8 did_swap false
No Swap
![Page 79: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/79.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
32
N 8 did_swap false
![Page 80: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/80.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
32
N 8 did_swap false
No Swap
![Page 81: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/81.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
33
N 8 did_swap false
![Page 82: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/82.jpg)
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
33
N 8 did_swap false
No Swap
![Page 83: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/83.jpg)
After Fifth Pass of Outer Loop
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
34
N 8 did_swap false
Finished fifth “Bubble Up”
![Page 84: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/84.jpg)
Finished “Early”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_doindex
34
N 8 did_swap false
We didn’t do any swapping,so all of the other elementsmust be correctly placed.
We can “skip” the last twopasses of the outer loop.
![Page 85: Bubble sort](https://reader035.vdocument.in/reader035/viewer/2022062522/587cb2b61a28ab38028b5033/html5/thumbnails/85.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