bubble sort - southern methodist university › ... › lecture-5 › lecture-bubble-sort.pdf ·...
TRANSCRIPT
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” -
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
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
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
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
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
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
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
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.
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 )
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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