cs1022 computer programming & principles lecture 2.2 algorithms
DESCRIPTION
Bubble SortTRANSCRIPT
![Page 1: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/1.jpg)
CS1022 Computer Programming &
Principles
Lecture 2.2Algorithms
![Page 2: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/2.jpg)
Plan of lecture• Examples of sorting algorithms
2CS1022
![Page 3: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/3.jpg)
Bubble Sort
![Page 4: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/4.jpg)
Sorting• Sorting takes an unordered collection and makes
it an ordered one.
512354277 101
5 12 35 42 77 101
1 2 3 4 5 6
1 2 3 4 5 6
![Page 5: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
![Page 6: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
512354277 101Swap42 77
1 2 3 4 5 6
![Page 7: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
512357742 101Swap35 77
1 2 3 4 5 6
![Page 8: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
512773542 101Swap12 77
1 2 3 4 5 6
![Page 9: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
No need to swap
1 2 3 4 5 6
![Page 10: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
577123542 101 Swap5 101
1 2 3 4 5 6
![Page 11: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
77123542 5 101
Largest value correctly placed
1 2 3 4 5 6
![Page 12: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/12.jpg)
The “Bubble Up” Algorithmbeginindex <- 1last_compare_at <- n – 1
while not(index > last_compare_at)
if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1])
index <- index + 1end
![Page 13: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/13.jpg)
No, Swap isn’t built in.beginSwap(a, b)
t <- aa <- bb <- t
end
![Page 14: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/14.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 15: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/15.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 16: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/16.jpg)
“Bubbling” All the Elements
77123542 5 101
5421235 77 101
42 5 3512 77 101
42 35 512 77 101
42 35 12 5 77 101
N -
1
1 2 3 4 5 6
![Page 17: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/17.jpg)
Reducing the Number of Comparisons
12 35 42 77 101 5
77123542 5 101
5421235 77 101
42 5 3512 77 101
42 35 512 77 101
![Page 18: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/18.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 77 101
![Page 19: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/19.jpg)
Putting It All Together
![Page 20: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/20.jpg)
begin
to_do, index to_do <- N – 1
while not (to_do = 0) index <- 1 while not (index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) index <- index + 1 to_do <- to_do – 1
end
![Page 21: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/21.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 22: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/22.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 23: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/23.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 24: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/24.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 25: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/25.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 26: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/26.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 27: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/27.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 28: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/28.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 29: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/29.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 30: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/30.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 31: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/31.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 32: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/32.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 33: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/33.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 34: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/34.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 35: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/35.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 36: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/36.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 37: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/37.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 38: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/38.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 39: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/39.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 40: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/40.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 41: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/41.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 42: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/42.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 43: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/43.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 44: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/44.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 45: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/45.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 46: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/46.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 47: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
2
N 8 did_swap false
Swap
![Page 48: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/48.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 49: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/49.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 50: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/50.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 51: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
3
N 8 did_swap true
Swap
![Page 52: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/52.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 53: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/53.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 54: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
5
N 8 did_swap true
![Page 55: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
5
N 8 did_swap true
Swap
![Page 56: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/56.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 57: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/57.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 58: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/58.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 59: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/59.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 60: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/60.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 61: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/61.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 62: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/62.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 63: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/63.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 64: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/64.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 65: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/65.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 66: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/66.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 67: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/67.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 68: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/68.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 69: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
4
N 8 did_swap true
![Page 70: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
4
N 8 did_swap true
Swap
![Page 71: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/71.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 72: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/72.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 73: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/73.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 74: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/74.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 75: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/75.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 76: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/76.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 77: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/77.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 78: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
1
N 8 did_swap true
Swap
![Page 79: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/79.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 80: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/80.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 81: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
3
N 8 did_swap true
![Page 82: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
3
N 8 did_swap true
No Swap
![Page 83: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
4
N 8 did_swap true
![Page 84: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
4
N 8 did_swap true
No Swap
![Page 85: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/85.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 86: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/86.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 87: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/87.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 88: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/88.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 89: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
2
N 8 did_swap false
No Swap
![Page 90: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
3
N 8 did_swap false
![Page 91: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/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
3
N 8 did_swap false
No Swap
![Page 92: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/92.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 93: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/93.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.
![Page 94: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/94.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 95: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/95.jpg)
Truth in CS Act• NOBODY EVER USES BUBBLE SORT BECAUSE IT IS EXTREMELY INEFFICIENT
![Page 96: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/96.jpg)
Mergesort
![Page 97: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/97.jpg)
Sorting
• Sorting takes an unordered collection and makes it an ordered one.
512354277 101
5 12 35 42 77 101
![Page 98: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/98.jpg)
Divide and Conquer• Divide and Conquer cuts the problem in half each time, but uses the result of both halves:– cut the problem in half until the problem is trivial – solve for both halves– combine the solutions
![Page 99: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/99.jpg)
Mergesort• A divide-and-conquer algorithm:• Divide the unsorted array into 2 halves until the
sub-arrays only contain one element• Merge the sub-problem solutions together:– Compare the sub-array's first elements– Remove the smallest element and put it into
the result array– Continue the process until all elements have
been put into the result array
![Page 100: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/100.jpg)
Algorithm
Mergesort(Passed an array) if array size > 1
Divide array in half Call Mergesort on first half. Call Mergesort on second half. Merge two halves.
Merge(Passed two arrays) Compare leading element in each array Select lower and place in new array. (If one input array is empty then place remainder of other array in output array)
![Page 101: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/101.jpg)
674523 14 6 3398 42
![Page 102: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/102.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
![Page 103: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/103.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
![Page 104: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/104.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398
![Page 105: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/105.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398
Merge
![Page 106: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/106.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398
23
Merge
![Page 107: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/107.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398
23 98
Merge
![Page 108: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/108.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
23 98
![Page 109: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/109.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
23 98
![Page 110: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/110.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
14
Merge
23 98
![Page 111: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/111.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
45
Merge
23 98 14
![Page 112: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/112.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
98 451423
![Page 113: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/113.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
98 14
14
23 45
![Page 114: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/114.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
23 14
14 23
98 45
![Page 115: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/115.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
23 98 4514
14 23 45
![Page 116: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/116.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
Merge
23 98 4514
14 23 45 98
![Page 117: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/117.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
23 98 4514
14 23 45 98
![Page 118: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/118.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676
23 98 4514
14 23 45 98
![Page 119: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/119.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676
Merge
23 98 4514
14 23 45 98
![Page 120: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/120.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676
6
Merge
23 98 4514
14 23 45 98
![Page 121: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/121.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676
67
Merge
23 98 4514 6
14 23 45 98
![Page 122: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/122.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
23 98 4514 676
14 23 45 98
![Page 123: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/123.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676
14 23 45 98
![Page 124: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/124.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
3323 98 4514 676
14 23 45 98
![Page 125: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/125.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
4223 98 4514 676 33
14 23 45 98
![Page 126: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/126.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 45 98
![Page 127: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/127.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 6 4233
14 23 45 98 6
67
![Page 128: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/128.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 6 33
14 23 45 98 6 33
67 42
![Page 129: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/129.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 6 4233
14 23 45 98 6 33 42
67
![Page 130: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/130.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 45 98 6 33 42 67
![Page 131: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/131.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
23 45 98 33 42 6714 6
![Page 132: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/132.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
23 45 98 6 42 67
6
14 33
![Page 133: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/133.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 45 98 6 42 67
6 14
23 33
![Page 134: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/134.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 98 6 42 67
6 14 23
45 33
![Page 135: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/135.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 98 6 33 67
6 14 23 33
45 42
![Page 136: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/136.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 98 6 33 42
6 14 23 33 42
45 67
![Page 137: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/137.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 45 6 33 42
6 14 23 33 42 45
98 67
![Page 138: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/138.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
![Page 139: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/139.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
Merge
23 98 4514 676 4233
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
![Page 140: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/140.jpg)
674523 14 6 3398 42
674523 14 6 3398 42
4523 1498
2398 45 14
676 33 42
676 33 42
23 98 4514 676 4233
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
![Page 141: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/141.jpg)
674523 14 6 3398 42
6 14 23 33 42 45 67 98
![Page 142: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/142.jpg)
Summary• Divide the unsorted collection into two • Until the sub-arrays only contain one element
• Then merge the sub-problem solutions together
![Page 143: CS1022 Computer Programming & Principles Lecture 2.2 Algorithms](https://reader036.vdocuments.net/reader036/viewer/2022062504/5a4d1b467f8b9ab0599a369d/html5/thumbnails/143.jpg)
Summary: what you now know
• Some examples of sorting using algorithms.• Some issues about how long they might be.• Some intuitions about correctness• Thanks to Leahy and Smith (2000)
143
CS1022