P4 Sec 4.1.2) Bubble Sort & Insertion Sort Algorithms & VB ...
Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf ·...
Transcript of Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf ·...
![Page 1: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/1.jpg)
2/1/2016
1
CS 3353
Lecture 5
Vidroha debroyMaterial adapted courtesy of Prof. Xiangnan Kong and Prof.
Carolina Ruiz at Worcester Polytechnic Institute
Searching for a number
54
68
3
103
73
131
26
88
3
91
19
Find a specific number “91” -
![Page 2: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/2.jpg)
2/1/2016
2
Searching for a number
54
68
3
103
73
131
26
88
13
91
19
Find a specific number “91” - what about now?
73
38371
17
35
41
85
114
2161
87
9711
49
94
839
57
66
12
22
11163
102
9976
Why Sorting?
• Practical Application
– People by last name
– Countries by population
– Search engine results by relevance
• Fundamental to other algorithms
• Different algorithms have different asymtotic and constant-factor trade-offs
– no single “best” sort for all scenarios
– knowing one way to sort just isn't enough
Keeping data in “order” allows it to be searched
more efficiently
![Page 3: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/3.jpg)
2/1/2016
3
Sorting
• Sorting: Rearranging the values in an array or collection into a specific order (usually into their "natural ordering").
– one of the fundamental problems in computer science
– it is estimated that 25~50% of all computing power is used for sorting activities.
• Popular Sorting algorithms:
– bubble sort: swap adjacent pairs that are out of order
– selection sort: look for the smallest element, move to front
– insertion sort: build an increasingly large sorted front portion
– merge sort: recursively divide the array in half and sort it
– heap sort: place the values into a sorted tree structure
– quick sort: recursively partition array based on a middle value
Sorting Example
Bubble Sort
![Page 4: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/4.jpg)
2/1/2016
4
Problem Definition
• 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
Input:
Output:
One Idea: Bubble
1
Water
Air
2
3
1
3
2
1
2
3
1
2
3
![Page 5: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/5.jpg)
2/1/2016
5
"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
"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 - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/6.jpg)
2/1/2016
6
"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
"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 7: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/7.jpg)
2/1/2016
7
"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
"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 8: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/8.jpg)
2/1/2016
8
"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
The “Bubble Up” Algorithm
Bubble-Sort-step1( A ):
for k = 1 … (N – 1):if A[k] > A[k+1]:
Swap( A, k, k+1 )
Swap( A, x, y ):tmp = A[x]A[x] = A[y]A[y] = tmp
![Page 9: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/9.jpg)
2/1/2016
9
Need More Iterations
• 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
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 10: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/10.jpg)
2/1/2016
10
“Bubbling” All the Elements
77123542 5
1 2 3 4 5 6
101
5421235 77
1 2 3 4 5 6
101
4253512 77
1 2 3 4 5 6
101
4235512 77
1 2 3 4 5 6
101
4235125 77
1 2 3 4 5 6
101
N -
1
The “Bubble Up” Algorithm
Bubble-Sort( A ):
for k = 1 … (N – 1):if A[k] > A[k+1]:
Swap( A, k, k+1 )
![Page 11: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/11.jpg)
2/1/2016
11
The “Bubble Up” Algorithm
Bubble-Sort( A ):
for i = 1 … (N – 1):for k = 1 … (N – 1):
if A[k] > A[k+1]:Swap( A, k, k+1 )
Bubble-Sort( A ):
for i = 1 … (N – 1):
for k = 1 … (N – 1):if A[k] > A[k+1]:
Swap( A, k, k+1 )
The “Bubble Up” Algorithm
Inn
er
loo
p
Ou
ter
loo
p
To bubble a value
To do N-1 iterations
![Page 12: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/12.jpg)
2/1/2016
12
Reducing the Number of Comparisons
12354277 101
1 2 3 4 5 6
5
77123542 5
1 2 3 4 5 6
101
5421235 77
1 2 3 4 5 6
101
4253512 77
1 2 3 4 5 6
101
4235512 77
1 2 3 4 5 6
101
Reducing the Number of Comparisons
• Assume the array size N
• On the ith iteration, we only need to do N-i comparisons.
• For example:
– N = 6
– i = 4 (4th iteration)
– Thus, we have 2 comparisons to do
4253512 77
1 2 3 4 5 6
101
![Page 13: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/13.jpg)
2/1/2016
13
Bubble-Sort( A ):
for i = 1 … (N – 1):for k = 1 … (N – 1):
if A[k] > A[k+1]:Swap( A, k, k+1 )
The “Bubble Up” Algorithm
Bubble-Sort( A ):
for i = 1 … (N – 1):for k = 1 … (N – i):
if A[k] > A[k+1]:Swap( A, k, k+1 )
The “Bubble Up” Algorithm
![Page 14: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/14.jpg)
2/1/2016
14
Code Demo
An Animated Example
674523 14 6 3398 42
1 2 3 4 5 6 7 8
i
k
1
N 8
![Page 15: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/15.jpg)
2/1/2016
15
An Animated Example
674523 14 6 3398 42
1 2 3 4 5 6 7 8
1
1
N 8
i
k
An Animated Example
674523 14 6 3398 42
1 2 3 4 5 6 7 8
1
1
N 8
Swap
i
k
![Page 16: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/16.jpg)
2/1/2016
16
An Animated Example
674598 14 6 3323 42
1 2 3 4 5 6 7 8
1
1
N 8
Swap
i
k
An Animated Example
674598 14 6 3323 42
1 2 3 4 5 6 7 8
1
2
N 8
i
k
![Page 17: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/17.jpg)
2/1/2016
17
An Animated Example
674598 14 6 3323 42
1 2 3 4 5 6 7 8
1
2
N 8
Swap
i
k
An Animated Example
679845 14 6 3323 42
1 2 3 4 5 6 7 8
1
2
N 8
Swap
i
k
![Page 18: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/18.jpg)
2/1/2016
18
An Animated Example
679845 14 6 3323 42
1 2 3 4 5 6 7 8
1
3
N 8
i
k
An Animated Example
679845 14 6 3323 42
1 2 3 4 5 6 7 8
1
3
N 8
Swap
i
k
![Page 19: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/19.jpg)
2/1/2016
19
An Animated Example
671445 98 6 3323 42
1 2 3 4 5 6 7 8
1
3
N 8
Swap
i
k
An Animated Example
671445 98 6 3323 42
1 2 3 4 5 6 7 8
1
4
N 8
i
k
![Page 20: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/20.jpg)
2/1/2016
20
An Animated Example
671445 98 6 3323 42
1 2 3 4 5 6 7 8
1
4
N 8
Swap
i
k
An Animated Example
671445 6 98 3323 42
1 2 3 4 5 6 7 8
1
4
N 8
Swap
i
k
![Page 21: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/21.jpg)
2/1/2016
21
An Animated Example
671445 6 98 3323 42
1 2 3 4 5 6 7 8
1
5
N 8
i
k
An Animated Example
671445 6 98 3323 42
1 2 3 4 5 6 7 8
1
5
N 8
Swap
i
k
![Page 22: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/22.jpg)
2/1/2016
22
An Animated Example
981445 6 67 3323 42
1 2 3 4 5 6 7 8
1
5
N 8
Swap
i
k
An Animated Example
981445 6 67 3323 42
1 2 3 4 5 6 7 8
1
6
N 8
i
k
![Page 23: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/23.jpg)
2/1/2016
23
An Animated Example
981445 6 67 3323 42
1 2 3 4 5 6 7 8
1
6
N 8
Swap
i
k
An Animated Example
331445 6 67 9823 42
1 2 3 4 5 6 7 8
1
6
N 8
Swap
i
k
![Page 24: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/24.jpg)
2/1/2016
24
An Animated Example
331445 6 67 9823 42
1 2 3 4 5 6 7 8
1
7
N 8
i
k
An Animated Example
331445 6 67 9823 42
1 2 3 4 5 6 7 8
1
7
N 8
Swap
i
k
![Page 25: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/25.jpg)
2/1/2016
25
An Animated Example
331445 6 67 4223 98
1 2 3 4 5 6 7 8
1
7
N 8
Swap
i
k
After First Pass of Outer Loop
331445 6 67 4223 98
1 2 3 4 5 6 7 8
1
8
N 8
Finished first “Bubble Up”
i
k
![Page 26: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/26.jpg)
2/1/2016
26
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
2
1
N 8
i
k
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
2
1
N 8
No Swap
i
k
![Page 27: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/27.jpg)
2/1/2016
27
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
2
2
N 8
i
k
The Second “Bubble Up”
331445 6 67 4223 98
1 2 3 4 5 6 7 8
2
2
N 8
Swap
i
k
![Page 28: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/28.jpg)
2/1/2016
28
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
2
2
N 8
Swap
i
k
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
2
3
N 8
i
k
![Page 29: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/29.jpg)
2/1/2016
29
The Second “Bubble Up”
334514 6 67 4223 98
1 2 3 4 5 6 7 8
2
3
N 8
Swap
i
k
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
2
3
N 8
Swap
i
k
![Page 30: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/30.jpg)
2/1/2016
30
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
2
4
N 8
i
k
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
2
4
N 8
No Swap
i
k
![Page 31: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/31.jpg)
2/1/2016
31
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
2
5
N 8
i
k
The Second “Bubble Up”
33614 45 67 4223 98
1 2 3 4 5 6 7 8
2
5
N 8
Swap
i
k
![Page 32: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/32.jpg)
2/1/2016
32
The Second “Bubble Up”
67614 45 33 4223 98
1 2 3 4 5 6 7 8
2
5
N 8
Swap
i
k
The Second “Bubble Up”
67614 45 33 4223 98
1 2 3 4 5 6 7 8
2
6
N 8
i
k
![Page 33: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/33.jpg)
2/1/2016
33
The Second “Bubble Up”
67614 45 33 4223 98
1 2 3 4 5 6 7 8
2
6
N 8
Swap
i
k
The Second “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
2
6
N 8
Swap
i
k
![Page 34: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/34.jpg)
2/1/2016
34
After Second Pass of Outer Loop
42614 45 33 6723 98
1 2 3 4 5 6 7 8
2
7
N 8
Finished second “Bubble Up”
i
k
The Third “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
3
1
N 8
i
k
![Page 35: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/35.jpg)
2/1/2016
35
The Third “Bubble Up”
42614 45 33 6723 98
1 2 3 4 5 6 7 8
3
1
N 8
Swap
i
k
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
3
1
N 8
Swap
i
k
![Page 36: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/36.jpg)
2/1/2016
36
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
3
2
N 8
i
k
The Third “Bubble Up”
42623 45 33 6714 98
1 2 3 4 5 6 7 8
3
2
N 8
Swap
i
k
![Page 37: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/37.jpg)
2/1/2016
37
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
3
2
N 8
Swap
i
k
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
3
3
N 8
i
k
![Page 38: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/38.jpg)
2/1/2016
38
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
3
3
N 8
No Swap
i
k
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
3
4
N 8
i
k
![Page 39: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/39.jpg)
2/1/2016
39
The Third “Bubble Up”
42236 45 33 6714 98
1 2 3 4 5 6 7 8
3
4
N 8
Swap
i
k
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
3
4
N 8
Swap
i
k
![Page 40: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/40.jpg)
2/1/2016
40
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
3
5
N 8
i
k
The Third “Bubble Up”
42236 33 45 6714 98
1 2 3 4 5 6 7 8
3
5
N 8
Swap
i
k
![Page 41: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/41.jpg)
2/1/2016
41
The Third “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
3
5
N 8
Swap
i
k
After Third Pass of Outer Loop
45236 33 42 6714 98
1 2 3 4 5 6 7 8
3
6
N 8
Finished third “Bubble Up”
i
k
![Page 42: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/42.jpg)
2/1/2016
42
The Fourth “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
4
1
N 8
i
k
The Fourth “Bubble Up”
45236 33 42 6714 98
1 2 3 4 5 6 7 8
4
1
N 8
Swap
i
k
![Page 43: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/43.jpg)
2/1/2016
43
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
4
1
N 8
Swap
i
k
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
4
2
N 8
i
k
![Page 44: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/44.jpg)
2/1/2016
44
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
4
2
N 8
No Swap
i
k
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
4
3
N 8
i
k
![Page 45: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/45.jpg)
2/1/2016
45
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
4
3
N 8
No Swap
i
k
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
4
4
N 8
i
k
![Page 46: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/46.jpg)
2/1/2016
46
The Fourth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
4
4
N 8
No Swap
i
k
After Fourth Pass of Outer Loop
452314 33 42 676 98
1 2 3 4 5 6 7 8
4
5
N 8
Finished fourth “Bubble Up”
i
k
![Page 47: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/47.jpg)
2/1/2016
47
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
5
1
N 8
i
k
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
5
1
N 8
No Swap
i
k
![Page 48: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/48.jpg)
2/1/2016
48
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
5
2
N 8
i
k
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
5
2
N 8
No Swap
i
k
![Page 49: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/49.jpg)
2/1/2016
49
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
5
3
N 8
i
k
The Fifth “Bubble Up”
452314 33 42 676 98
1 2 3 4 5 6 7 8
5
3
N 8
No Swap
i
k
![Page 50: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/50.jpg)
2/1/2016
50
After Fifth Pass of Outer Loop
452314 33 42 676 98
1 2 3 4 5 6 7 8
5
4
N 8
Finished fifth “Bubble Up”
i
k
452314 33 42 676 98
1 2 3 4 5 6 7 8
5
4
N 8
i
k
![Page 51: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/51.jpg)
2/1/2016
51
Analysis of Bubble Sort and Loop Invariant
Bubble-Sort( A ):
for i = 1 … (N – 1):
for k = 1 … (N – 1):if A[k] > A[k+1]:
Swap( A, k, k+1 )
Bubble Sort Algorithm
Inn
er
loo
p
Ou
ter
loo
p
To bubble a value
To do N-1 iterations
![Page 52: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/52.jpg)
2/1/2016
52
Loop Invariant• It is a condition or property that is guaranteed to be correct with each
iteration in the loop
• Usually used to prove the correctness of the algorithm
for i = 1 … (N – 1):
for k = 1 … (N – 1):if A[k] > A[k+1]:
Swap( A, k, k+1 )
Inn
er
loo
p
Ou
ter
loo
p
To bubble a value
To do N-1 iterations
Loop Invariant for Bubble Sort
• By the end of iteration i the right-most i items (largest) are sorted and in place
for i = 1 … (N – 1):
for k = 1 … (N – 1):if A[k] > A[k+1]:
Swap( A, k, k+1 )
Inn
er
loo
p
Ou
ter
loo
p
To bubble a value
To do N-1 iterations
![Page 53: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/53.jpg)
2/1/2016
53
N-1 Iterations
1st
2nd
3rd
4th
77123542 5
1 2 3 4 5 6
101
5421235 77
1 2 3 4 5 6
101
4253512 77
1 2 3 4 5 6
101
4235512 77
1 2 3 4 5 6
101
4235125 77
1 2 3 4 5 6
101
N -
1
5th
Correctness of Bubble Sort (using Loop Invariant)
• Bubble sort has N-1 Iterations
• Invariant: By the end of iteration i the right-most i items (largest) are sorted and in place
• Then: After the N-1 iterations The right-most N-1 items are sorted
– This implies that all the N items are sorted
![Page 54: Bubble Sort - Southern Methodist University › ... › Lecture-5 › Lecture-Bubble-Sort.pdf · 2016-02-02 · –bubble sort: swap adjacent pairs that are out of order –selection](https://reader033.fdocuments.us/reader033/viewer/2022060509/5f24ef21ccad1b7f4417eeea/html5/thumbnails/54.jpg)
2/1/2016
54
Bubble Sort Time Complexity
• Best-Case Time Complexity
– The scenario under which the algorithm will do the least amount of work (finish the fastest)
– What if we had a pass and nothing was swapped? What does that tell us?
• Worst-Case Time Complexity
– The scenario under which the algorithm will do the largest amount of work (finish the slowest)
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
• Not the most efficient algorithm around but is easy to understand