An Animated Example
to_do
index
did_swap
true
Swap
23 45 14 6
1
67 33 98 42
5
An Animated Example
N
to_do
index
did_swap
23 45 14 6
1
true
67 33 98 42
5
An Animated Example
N
to_do
index
did_swap
true
Swap
23 45 14 6
1
67 33 98 42
5
An Animated Example
N
to_do
index
did_swap
true
Swap
23 45 14 6
1
67 33 42 98
5
After First Pass of Outer Loop
N
to_do
index
did_swap
Finished first Bubble Up
23 45 14 6
1
true
67 33 42 98
5
The Second Bubble Up
N
to_do
index
did_swap false
23 45 14 6
1
67 33 42 98
5
The Second Bubble Up
N
to_do
index
did_swap false
No Swap
23 45 14 6
1
67 33 42 98
5
The Second Bubble Up
N
to_do
index
did_swap false
23 45 14 6
1
67 33 42 98
5
The Second Bubble Up
N
to_do
index
did_swap false
Swap
23 45 14 6
1
67 33 42 98
5
The Second Bubble Up
N
to_do
index
did_swap
true
Swap
23 14 45 6
1
67 33 42 98
5
The Second Bubble Up
N
to_do
index
did_swap
23 14 45 6
1
true
67 33 42 98
5
The Second Bubble Up
N
to_do
index
did_swap
true
Swap
23 14 45 6
1
67 33 42 98
5
The Second Bubble Up
N
to_do
index
did_swap
true
Swap
23 14 6
1
45 67 33 42 98
4
The Second Bubble Up
N
to_do
index
did_swap
23 14 6
1
true
45 67 33 42 98
4
The Second Bubble Up
N
to_do
index
did_swap
true
No Swap
23 14 6
1
45 67 33 42 98
4
The Second Bubble Up
N
to_do
index
did_swap
23 14 6
1
true
45 67 33 42 98
4
The Second Bubble Up
N
to_do
index
did_swap
true
Swap
23 14 6
1
45 67 33 42 98
4
The Second Bubble Up
N
to_do
index
did_swap
true
Swap
23 14 6
1
45 33 67 42 98
4
The Second Bubble Up
N
to_do
index
did_swap
23 14 6
1
true
45 33 67 42 98
4
The Second Bubble Up
N
to_do
index
did_swap
true
Swap
23 14 6
1
45 33 67 42 98
4
The Second Bubble Up
N
to_do
index
did_swap
true
Swap
23 14 6
1
45 33 42 67 98
4
After Second Pass of Outer Loop
N
to_do
index
did_swap
Finished second Bubble Up
23 14 6
1
true
45 33 42 67 98
4
The Third Bubble Up
N
to_do
index
did_swap false
23 14 6
1
45 33 42 67 98
4
The Third Bubble Up
N
to_do
index
did_swap false
Swap
23 14 6
1
45 33 42 67 98
4
The Third Bubble Up
N
to_do
index
did_swap
true
Swap
14 23 6
1
45 33 42 67 98
4
The Third Bubble Up
N
to_do
index
did_swap
14 23 6
1
true
45 33 42 67 98
4
The Third Bubble Up
N
to_do
index
did_swap
true
Swap
14 23 6
1
45 33 42 67 98
4
The Third Bubble Up
N
to_do
index
did_swap
true
Swap
14 6
1
23 45 33 42 67 98
3
The Third Bubble Up
N
to_do
index
did_swap
14 6
1
true
23 45 33 42 67 98
3
The Third Bubble Up
N
to_do
index
did_swap
true
No Swap
14 6
1
23 45 33 42 67 98
3
The Third Bubble Up
N
to_do
index
did_swap
14 6
1
true
23 45 33 42 67 98
3
The Third Bubble Up
N
to_do
index
did_swap
true
Swap
14 6
1
23 45 33 42 67 98
3
The Third Bubble Up
N
to_do
index
did_swap
true
Swap
14 6
1
23 33 45 42 67 98
3
The Third Bubble Up
N
to_do
index
did_swap
14 6
1
true
23 33 45 42 67 98
3
The Third Bubble Up
N
to_do
index
did_swap
true
Swap
14 6
1
23 33 45 42 67 98
3
The Third Bubble Up
N
to_do
index
did_swap
true
Swap
14 6
1
23 33 42 45 67 98
3
After Third Pass of Outer Loop
N
to_do
index
did_swap
Finished third Bubble Up
14 6
1
true
23 33 42 45 67 98
3
The Fourth Bubble Up
N
to_do
index
did_swap false
14 6
1
23 33 42 45 67 98
3
The Fourth Bubble Up
N
to_do
index
did_swap false
Swap
14 6
1
23 33 42 45 67 98
3
The Fourth Bubble Up
N
to_do
index
did_swap
true
Swap
6
1
14 23 33 42 45 67 98
2
The Fourth Bubble Up
N
to_do
index
6
1
did_swap
true
14 23 33 42 45 67 98
2
The Fourth Bubble Up
N
to_do
index
did_swap
true
No Swap
6
1
14 23 33 42 45 67 98
2
The Fourth Bubble Up
N
to_do
index
6
1
did_swap
true
14 23 33 42 45 67 98
2
The Fourth Bubble Up
N
to_do
index
did_swap
true
No Swap
6
1
14 23 33 42 45 67 98
2
The Fourth Bubble Up
N
to_do
index
6
1
did_swap
true
14 23 33 42 45 67 98
2
The Fourth Bubble Up
N
to_do
index
did_swap
true
No Swap
6
1
14 23 33 42 45 67 98
2
After Fourth Pass of Outer Loop
N
to_do
index
6
1
did_swap
true
Finished fourth Bubble Up
14 23 33 42 45 67 98
2
The Fifth Bubble Up
N
to_do
index
6
1
did_swap false
14 23 33 42 45 67 98
2
The Fifth Bubble Up
N
to_do
index
did_swap false
No Swap
6
1
14 23 33 42 45 67 98
2
The Fifth Bubble Up
N
to_do
index
6
1
did_swap false
14 23 33 42 45 67 98
2
The Fifth Bubble Up
N
to_do
index
did_swap false
No Swap
6
1
14 23 33 42 45 67 98
2
The Fifth Bubble Up
N
to_do
index
6
1
did_swap false
14 23 33 42 45 67 98
2
The Fifth Bubble Up
N
to_do
index
did_swap false
No Swap
6
1
14 23 33 42 45 67 98
2
After Fifth Pass of Outer Loop
N
to_do
index
6
1
did_swap false
Finished fifth Bubble Up
14 23 33 42 45 67 98
2
Finished Early
N
to_do
index
did_swap false
We didnt do any swapping,
so all of the other elements
must be correctly placed.
We can skip the last two
passes of the outer loop.
6
1
14 23 33 42 45 67 98
2
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
LB
Truth in CS Act
NOBODY EVER USES BUBBLE SORT
NOBODY
NOT EVER
BECAUSE IT IS EXTREMELY INEFFICIENT
Questions?
Mergesort
Sorting
Sorting takes an unordered collection and
makes it an ordered one.
1
77
42
35
12
101
12
35
42
77
101
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
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-arrays 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
37
23
89
15
12
19
How to Remember Merge Sort?
Thats easy. Just
remember Mariah
Carey.
As a singing star, Ms.
Carey has perfected
the wax-on wave
motion--a clockwise
sweep of her hand
used to emphasize
lyrics.
The Maria
Wax-on Angle:
(,t)
The Siren of Subquadratic Sorts
How To Remember Merge Sort?
( ,t)
Just as Mariah recursively moves
her hands into smaller circles, so
too does merge sort recursively
split an array into smaller
segments.
We need two such recursions,
one for each half of the split
array.
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)
LB
More TRUTH in CS
We dont really pass in two arrays!
We pass in one array with indicator variables which
tell us where one set of data starts and finishes and
where the other set of data starts and finishes.
Honest.
s1
f1
s2
f2
LB
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)
98 23 45 14
6 67 33 42
98 23 45 14
98 23 45 14
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
45 14
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
45 14
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
Merge
45 14
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
23
Merge
45 14
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
Merge
45 14
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
45 14
45
14
23 98
Merge
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14
Merge
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
Merge
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
Merge
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14
Merge
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23
Merge
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45
Merge
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
Merge
6 67 33 42
6 67 33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
6
67
33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
6 67 33 42
6 67 33 42
6 67
6
67
14 45
14 23 45 98
Merge
33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
6
67
6
Merge
33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
6
67
6 67
Merge
33 42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
33 42
33
67
6 67
42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
33 42
33
67
42
6 67
Merge
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
33 42
33
67
6 67
42
33
Merge
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
33 42
33
67
6 67
42
33 42
Merge
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
6 67 33 42
6 67 33 42
6 67
33 42
33
67
6 67
42
33 42
14 23 45 98
Merge
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
33 42
33
67
6 67
42
33 42
6
Merge
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
33 42
33
67
6 67
42
33 42
6 33
Merge
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
33 42
33
67
6 67
42
33 42
6 33 42
Merge
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
33 42
33
67
6 67
42
33 42
6 33 42 67
Merge
98 23 45 14
6 67 33 42
98 23 45 14
98 23
98
23
23 98
6 67 33 42
45 14
45
14
6 67
33 42
33
14 45
67
6 67
33 42
6 33 42 67
14 23 45 98
Merge
42
98 23 45 14
6 67 33 42
98 23 45 14
98 23
98
23
23 98
6 67 33 42
45 14
45
14
6 67
33 42
33
14 45
67
6 67
33 42
6 33 42 67
14 23 45 98
6
Merge
42
98 23 45 14
6 67 33 42
98 23 45 14
98 23
98
23
6 67 33 42
45 14
45
23 98
14
6 67
33 42
33
14 45
67
6 67
33 42
6 33 42 67
14 23 45 98
6 14
Merge
42
98 23 45 14
6 67 33 42
98 23 45 14
98 23
98
23
23 98
6 67 33 42
45 14
45
14
6 67
33 42
33
14 45
67
6 67
33 42
6 33 42 67
14 23 45 98
6 14 23
Merge
42
98 23 45 14
6 67 33 42
98 23 45 14
98 23
98
23
23 98
6 67 33 42
45 14
45
14
6 67
33 42
33
14 45
67
6 67
33 42
6 33 42 67
14 23 45 98
6 14 23 33
Merge
42
98 23 45 14
6 67 33 42
98 23 45 14
98 23
98
23
23 98
6 67 33 42
45 14
45
14
6 67
33 42
33
14 45
67
6 67
33 42
6 33 42 67
14 23 45 98
6 14 23 33 42
Merge
42
98 23 45 14
6 67 33 42
98 23 45 14
98 23
98
23
23 98
6 67 33 42
45 14
45
14
6 67
33 42
33
14 45
67
6 67
33 42
6 33 42 67
14 23 45 98
6 14 23 33 42 45
Merge
42
98 23 45 14
6 67 33 42
98 23 45 14
98 23
98
23
23 98
6 67 33 42
45 14
45
14
6 67
33 42
33
14 45
67
6 67
33 42
6 33 42 67
14 23 45 98
6 14 23 33 42 45 67
Merge
42
98 23 45 14
6 67 33 42
98 23 45 14
98 23
98
23
23 98
6 67 33 42
45 14
45
14
6 67
33 42
33
14 45
67
6 67
33 42
6 33 42 67
14 23 45 98
6 14 23 33 42 45 67 98
Merge
42
98 23 45 14
98 23 45 14
98 23
98
23
23 98
45 14
45
14
14 45
14 23 45 98
6 67 33 42
6 67 33 42
6 67
33 42
33
67
6 67
42
33 42
6 33 42 67
6 14 23 33 42 45 67 98
98 23 45 14
6 67 33 42
6 14 23 33 42 45 67 98
Summary
Divide the unsorted collection into two
Until the sub-arrays only contain one
element
Then merge the sub-problem solutions
together
Questions?
Review?