bubble sort merge sort. bubble sort sorting sorting takes an unordered collection and makes it an...
TRANSCRIPT
![Page 1: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/1.jpg)
Bubble SortMerge Sort
![Page 2: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/2.jpg)
Bubble Sort
![Page 3: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/3.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 4: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/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 6
![Page 5: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/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 101
1 2 3 4 5 6
Swap42 77
![Page 6: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/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 101
1 2 3 4 5 6
Swap35 77
![Page 7: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/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 101
1 2 3 4 5 6
Swap12 77
![Page 8: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/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 6
No need to swap
![Page 9: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/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 101
1 2 3 4 5 6
Swap5 101
![Page 10: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/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
1 2 3 4 5 6
101
Largest value correctly placed
![Page 11: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/11.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 12: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/12.jpg)
No, Swap isn’t built in.
Procedure Swap(a, b isoftype in/out Num)
t isoftype Num
t <- a
a <- b
b <- t
endprocedure // Swap
LB
![Page 13: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/13.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 14: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/14.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 15: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/15.jpg)
“Bubbling” All the Elements
77123542 51 2 3 4 5 6
101
5421235 771 2 3 4 5 6
101
42 5 3512 771 2 3 4 5 6
101
42 35 512 771 2 3 4 5 6
101
42 35 12 5 771 2 3 4 5 6
101
N -
1
![Page 16: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/16.jpg)
Reducing the Number of Comparisons
12 35 42 77 1011 2 3 4 5 6
5
77123542 51 2 3 4 5 6
101
5421235 771 2 3 4 5 6
101
42 5 3512 771 2 3 4 5 6
101
42 35 512 771 2 3 4 5 6
101
![Page 17: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/17.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– Thus we have 2 comparisons to do
42 5 3512 771 2 3 4 5 6
101
![Page 18: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/18.jpg)
Putting It All Together
![Page 19: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/19.jpg)
N is … // Size of Array
Arr_Type definesa Array[1..N] of Num
Procedure Swap(n1, n2 isoftype in/out Num)
temp isoftype Num
temp <- n1
n1 <- n2
n2 <- temp
endprocedure // Swap
![Page 20: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/20.jpg)
procedure Bubblesort(A isoftype in/out Arr_Type) to_do, index isoftype Num to_do <- N – 1
loop exitif(to_do = 0) index <- 1 loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop to_do <- to_do - 1 endloopendprocedure // Bubblesort
Inn
er l
oo
p
Ou
ter
loo
p
![Page 21: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/21.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 771 2 3 4 5 6
101
![Page 22: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/22.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 23: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/23.jpg)
did_swap isoftype Boolean did_swap <- true
loop exitif ((to_do = 0) OR NOT(did_swap)) index <- 1 did_swap <- false loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) did_swap <- true endif index <- index + 1 endloop to_do <- to_do - 1 endloop
![Page 24: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/24.jpg)
An Animated Example
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
N 8 did_swap true
![Page 25: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/25.jpg)
An Animated Example
674523 14 6 3398 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8 did_swap false
![Page 26: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/26.jpg)
An Animated Example
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 27: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/27.jpg)
An Animated Example
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 28: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/28.jpg)
An Animated Example
674598 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8 did_swap true
![Page 29: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/29.jpg)
An Animated Example
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 30: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/30.jpg)
An Animated Example
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 31: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/31.jpg)
An Animated Example
679845 14 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8 did_swap true
![Page 32: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/32.jpg)
An Animated Example
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 33: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/33.jpg)
An Animated Example
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 34: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/34.jpg)
An Animated Example
671445 98 6 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8 did_swap true
![Page 35: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/35.jpg)
An Animated Example
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 36: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/36.jpg)
An Animated Example
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 37: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/37.jpg)
An Animated Example
671445 6 98 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8 did_swap true
![Page 38: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/38.jpg)
An Animated Example
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 39: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/39.jpg)
An Animated Example
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 40: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/40.jpg)
An Animated Example
981445 6 67 3323 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8 did_swap true
![Page 41: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/41.jpg)
An Animated Example
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 42: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/42.jpg)
An Animated Example
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 43: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/43.jpg)
An Animated Example
331445 6 67 9823 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8 did_swap true
![Page 44: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/44.jpg)
An Animated Example
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 45: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/45.jpg)
An Animated Example
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8
Swap
did_swap true
![Page 46: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/46.jpg)
After First Pass of Outer Loop
331445 6 67 4223 98
1 2 3 4 5 6 7 8
to_do
index
7
8
N 8
Finished first “Bubble Up”
did_swap true
![Page 47: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/47.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 48: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/48.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 49: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/49.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 50: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/50.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 51: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/51.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 52: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/52.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 53: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/53.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 54: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/54.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 55: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/55.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 56: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/56.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 57: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/57.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 58: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/58.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 59: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/59.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 60: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/60.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 61: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/61.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
Swap
![Page 62: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/62.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 63: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/63.jpg)
After Second Pass of Outer Loop
42614 45 33 6723 98
1 2 3 4 5 6 7 8
to_do
index
6
7
N 8 did_swap true
Finished second “Bubble Up”
![Page 64: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/64.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 65: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/65.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 66: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/66.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 67: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/67.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 68: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/68.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 69: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/69.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 70: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/70.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 71: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/71.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 72: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/72.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 73: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/73.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 74: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/74.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 75: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/75.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 76: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/76.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 77: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/77.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
![Page 78: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/78.jpg)
After Third Pass of Outer Loop
45236 33 42 6714 98
1 2 3 4 5 6 7 8
to_do
index
5
6
N 8 did_swap true
Finished third “Bubble Up”
![Page 79: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/79.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 80: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/80.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 81: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/81.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 82: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/82.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 83: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/83.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 84: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/84.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 85: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/85.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 86: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/86.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 87: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/87.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
![Page 88: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/88.jpg)
After Fourth Pass of Outer Loop
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
4
5
N 8 did_swap true
Finished fourth “Bubble Up”
![Page 89: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/89.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 90: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/90.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 91: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/91.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 92: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/92.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 93: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/93.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 94: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/94.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
![Page 95: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/95.jpg)
After Fifth Pass of Outer Loop
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
4
N 8 did_swap false
Finished fifth “Bubble Up”
![Page 96: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42](https://reader035.vdocuments.net/reader035/viewer/2022070400/56649f145503460f94c28a7d/html5/thumbnails/96.jpg)
Finished “Early”
452314 33 42 676 98
1 2 3 4 5 6 7 8
to_do
index
3
4
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.