5 Lecture Bubble Sort and Analysis
5 Lecture Bubble Sort and Analysis
Bubble sort
1
Sorting
Sorting takes an unordered
collection and makes it an ordered
one. 1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
2
What is Bubble Sort
Idea:
– Repeatedly pass through the array
– Swaps adjacent elements that are out of order
i
1 2 3 n
8 4 6 9 2 3 1
j
3
"Bubbling Up" the Largest
Element
77 42 35 12 101 5
4
"Bubbling Up" the Largest
Element
1 2 3 4 5 6
42 Swap 42
77 77 35 12 101 5
5
"Bubbling Up" the Largest
Element
42 7735 Swap 35
77 12 101 5
6
"Bubbling Up" the Largest
Element
42 35 12 Swap 12
77 77 101 5
7
"Bubbling Up" the Largest
Element
42 35 12 77 101 5
No need to swap
8
"Bubbling Up" the Largest
Element
42 35 12 77 5 Swap 101
101 5
9
"Bubbling Up" the Largest
Element
42 35 12 77 5 101
10
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
1 2 3 4 5 6
42 35 12 77 5 101
11
Repeat “Bubble Up” How
Many Times?
If we have N elements…
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
13
Reducing the Number of
Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
14
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 1we have
2 3
2 comparisons
4 5 6
to do
12 35 5 42 77 101
15
Already Sorted Array?
What if the array was already
sorted?
What if only a few elements were
out of place and after a couple of
“bubble ups,” the collection was
sorted?
1 2 3 4 5 6
We5
want12 to be
35 able
42 to detect
77 this
101
and “stop early”!
16
An Animated Example
N 8 did_swap true
to_do 7
index
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
17
An Animated Example
N 8 did_swap false
to_do 7
index 1
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
18
An Animated Example
N 8 did_swap false
to_do 7
index 1
Swap
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
19
An Animated Example
N 8 did_swap true
to_do 7
index 1
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
20
An Animated Example
N 8 did_swap true
to_do 7
index 2
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
21
An Animated Example
N 8 did_swap true
to_do 7
index 2
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
22
An Animated Example
N 8 did_swap true
to_do 7
index 2
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
23
An Animated Example
N 8 did_swap true
to_do 7
index 3
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
24
An Animated Example
N 8 did_swap true
to_do 7
index 3
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
25
An Animated Example
N 8 did_swap true
to_do 7
index 3
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
26
An Animated Example
N 8 did_swap true
to_do 7
index 4
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
27
An Animated Example
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
28
An Animated Example
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
29
An Animated Example
N 8 did_swap true
to_do 7
index 5
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
30
An Animated Example
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
31
An Animated Example
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
32
An Animated Example
N 8 did_swap true
to_do 7
index 6
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
33
An Animated Example
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
34
An Animated Example
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
35
An Animated Example
N 8 did_swap true
to_do 7
index 7
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
36
An Animated Example
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
37
An Animated Example
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
38
After First Pass of Outer
Loop
N 8 did_swap true
to_do 7
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
39
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 1
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
40
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 1
No Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
41
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 2
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
42
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 2
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
43
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 2
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
44
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
45
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
46
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
47
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 4
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
48
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 4
No Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
49
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
50
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
51
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
52
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
53
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
54
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
55
After Second Pass of Outer
Loop
N 8 did_swap true
to_do 6
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
56
The Third “Bubble Up”
N 8 did_swap false
to_do 5
index 1
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
57
The Third “Bubble Up”
N 8 did_swap false
to_do 5
index 1
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
58
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 1
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
59
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
60
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
61
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
62
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 3
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
63
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 3
No Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
64
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
65
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
66
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
67
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
68
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
69
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
70
The Third “Bubble Up”
N 8 did_swap true
to_do 5
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
71
The Fourth “Bubble Up”
N 8 did_swap false
to_do 4
index 1
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
72
The Fourth “Bubble Up”
N 8 did_swap false
to_do 4
index 1
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
73
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 1
Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
74
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
75
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
76
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
77
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
78
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 4
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
79
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 4
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
80
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
81
The Fourth “Bubble Up”
N 8 did_swap false
to_do 3
index 1
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
82
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 1
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
83
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
84
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
85
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
86
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
87
After Fifth Pass of Outer
Loop
N 8 did_swap false
to_do 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
88
Finished “Early”
N 8 did_swap false
to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
89
Bubble Sort (Example-2)
2 9 5 4 8 1 2 5 4 8 1 9 2 4 5 1 8 9 2 4 1 5 8 9 1 2 4 5 8 9
2 5 9 4 8 1 2 4 5 8 1 9 2 4 5 1 8 9 2 1 4 5 8 9
2 5 4 9 8 1 2 4 5 8 1 9 2 4 1 5 8 9
2 5 4 8 9 1 2 4 5 1 8 9
2 5 4 8 1 9
(a) 1st pass (b) 2nd pass (c) 3rd pass (d) 4th pass (e) 5th pass
n2 n
(n 1) (n 2) ... 2 1
2 2
90
Algorithm of Bubble Sort
end if
next
next
end proc
Using a Boolean “Flag”