SlideShare uma empresa Scribd logo
1 de 153
Bubble Sort
Merge Sort
Bubble Sort
Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
5
12
35
42
77 101
1 2 3 4 5 6
5 12 35 42 77 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
5
12
35
42
77 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
5
12
35
42
77 101
1 2 3 4 5 6
Swap
42 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
5
12
35
77
42 101
1 2 3 4 5 6
Swap
35 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
5
12
77
35
42 101
1 2 3 4 5 6
Swap
12 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
5
77
12
35
42 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
5
77
12
35
42 101
1 2 3 4 5 6
Swap
5 101
"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
77
12
35
42 5
1 2 3 4 5 6
101
Largest value correctly placed
The “Bubble Up” Algorithm
index <- 1
last_compare_at <- n – 1
loop
exitif(index > last_compare_at)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
No, Swap isn’t built in.
Procedure Swap(a, b isoftype in/out Num)
t isoftype Num
t <- a
a <- b
b <- t
endprocedure // Swap
LB
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
77
12
35
42 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.
“Bubbling” All the Elements
77
12
35
42 5
1 2 3 4 5 6
101
5
42
12
35 77
1 2 3 4 5 6
101
42
5
35
12 77
1 2 3 4 5 6
101
42
35
5
12 77
1 2 3 4 5 6
101
42
35
12
5 77
1 2 3 4 5 6
101
N
-
1
Reducing the Number of Comparisons
12
35
42
77 101
1 2 3 4 5 6
5
77
12
35
42 5
1 2 3 4 5 6
101
5
42
12
35 77
1 2 3 4 5 6
101
42
5
35
12 77
1 2 3 4 5 6
101
42
35
5
12 77
1 2 3 4 5 6
101
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
35
12 77
1 2 3 4 5 6
101
Putting It All Together
N is … // Size of Array
Arr_Type definesa Array[1..N] of Num
Procedure Swap(n1, n2 isoftype in/out Num)
temp isoftype Num
temp <- n1
n1 <- n2
n2 <- temp
endprocedure // Swap
procedure Bubblesort(A isoftype in/out Arr_Type)
to_do, index isoftype Num
to_do <- N – 1
loop
exitif(to_do = 0)
index <- 1
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure // Bubblesort
Inner
loop
Outer
loop
Already Sorted Collections?
• What if the collection was already sorted?
• What if only a few elements were out of place and
after a couple of “bubble ups,” the collection was
sorted?
• We want to be able to detect this
and “stop early”!
42
35
12
5 77
1 2 3 4 5 6
101
Using a Boolean “Flag”
• We can use a boolean variable to determine if any
swapping occurred during the “bubble up.”
• If no swapping occurred, then we know that the
collection is already sorted!
• This boolean “flag” needs to be reset after each
“bubble up.”
did_swap isoftype Boolean
did_swap <- true
loop
exitif ((to_do = 0) OR NOT(did_swap))
index <- 1
did_swap <- false
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
did_swap <- true
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
An Animated Example
67
45
23 14 6 33
98 42
1 2 3 4 5 6 7 8
to_do
index
7
N 8 did_swap true
An Animated Example
67
45
23 14 6 33
98 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8 did_swap false
An Animated Example
67
45
23 14 6 33
98 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8
Swap
did_swap false
An Animated Example
67
45
98 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8
Swap
did_swap true
An Animated Example
67
45
98 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8 did_swap true
An Animated Example
67
45
98 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8
Swap
did_swap true
An Animated Example
67
98
45 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8
Swap
did_swap true
An Animated Example
67
98
45 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8 did_swap true
An Animated Example
67
98
45 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8
Swap
did_swap true
An Animated Example
67
14
45 98 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8
Swap
did_swap true
An Animated Example
67
14
45 98 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8 did_swap true
An Animated Example
67
14
45 98 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8
Swap
did_swap true
An Animated Example
67
14
45 6 98 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8
Swap
did_swap true
An Animated Example
67
14
45 6 98 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8 did_swap true
An Animated Example
67
14
45 6 98 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8
Swap
did_swap true
An Animated Example
98
14
45 6 67 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8
Swap
did_swap true
An Animated Example
98
14
45 6 67 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8 did_swap true
An Animated Example
98
14
45 6 67 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8
Swap
did_swap true
An Animated Example
33
14
45 6 67 98
23 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8
Swap
did_swap true
An Animated Example
33
14
45 6 67 98
23 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8 did_swap true
An Animated Example
33
14
45 6 67 98
23 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8
Swap
did_swap true
An Animated Example
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8
Swap
did_swap true
After First Pass of Outer Loop
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
7
8
N 8
Finished first “Bubble Up”
did_swap true
The Second “Bubble Up”
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
1
N 8 did_swap false
The Second “Bubble Up”
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
1
N 8 did_swap false
No Swap
The Second “Bubble Up”
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap false
The Second “Bubble Up”
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap false
Swap
The Second “Bubble Up”
33
45
14 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap true
Swap
The Second “Bubble Up”
33
45
14 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
The Second “Bubble Up”
33
45
14 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
Swap
The Second “Bubble Up”
33
6
14 45 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
Swap
The Second “Bubble Up”
33
6
14 45 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
4
N 8 did_swap true
The Second “Bubble Up”
33
6
14 45 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
4
N 8 did_swap true
No Swap
The Second “Bubble Up”
33
6
14 45 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
The Second “Bubble Up”
33
6
14 45 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
Swap
The Second “Bubble Up”
67
6
14 45 33 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
Swap
The Second “Bubble Up”
67
6
14 45 33 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
The Second “Bubble Up”
67
6
14 45 33 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
Swap
The Second “Bubble Up”
42
6
14 45 33 67
23 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
Swap
After Second Pass of Outer Loop
42
6
14 45 33 67
23 98
1 2 3 4 5 6 7 8
to_do
index
6
7
N 8 did_swap true
Finished second “Bubble Up”
The Third “Bubble Up”
42
6
14 45 33 67
23 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap false
The Third “Bubble Up”
42
6
14 45 33 67
23 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap false
Swap
The Third “Bubble Up”
42
6
23 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap true
Swap
The Third “Bubble Up”
42
6
23 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
The Third “Bubble Up”
42
6
23 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
Swap
The Third “Bubble Up”
42
23
6 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
Swap
The Third “Bubble Up”
42
23
6 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
3
N 8 did_swap true
The Third “Bubble Up”
42
23
6 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
3
N 8 did_swap true
No Swap
The Third “Bubble Up”
42
23
6 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
The Third “Bubble Up”
42
23
6 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
Swap
The Third “Bubble Up”
42
23
6 33 45 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
Swap
The Third “Bubble Up”
42
23
6 33 45 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
The Third “Bubble Up”
42
23
6 33 45 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
Swap
The Third “Bubble Up”
45
23
6 33 42 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
Swap
After Third Pass of Outer Loop
45
23
6 33 42 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
6
N 8 did_swap true
Finished third “Bubble Up”
The Fourth “Bubble Up”
45
23
6 33 42 67
14 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap false
The Fourth “Bubble Up”
45
23
6 33 42 67
14 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap false
Swap
The Fourth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap true
Swap
The Fourth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
2
N 8 did_swap true
The Fourth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
2
N 8 did_swap true
No Swap
The Fourth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
3
N 8 did_swap true
The Fourth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
3
N 8 did_swap true
No Swap
The Fourth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
4
N 8 did_swap true
The Fourth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
4
N 8 did_swap true
No Swap
After Fourth Pass of Outer Loop
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
5
N 8 did_swap true
Finished fourth “Bubble Up”
The Fifth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
1
N 8 did_swap false
The Fifth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
1
N 8 did_swap false
No Swap
The Fifth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
2
N 8 did_swap false
The Fifth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
2
N 8 did_swap false
No Swap
The Fifth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
3
N 8 did_swap false
The Fifth “Bubble Up”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
3
N 8 did_swap false
No Swap
After Fifth Pass of Outer Loop
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
4
N 8 did_swap false
Finished fifth “Bubble Up”
Finished “Early”
45
23
14 33 42 67
6 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 elements
must be correctly placed.
We can “skip” the last two
passes of the outer loop.
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
Truth in CS Act
• NOBODY EVER USES BUBBLE SORT
• NOBODY
• NOT EVER
• BECAUSE IT IS EXTREMELY INEFFICIENT
LB
Questions?
Mergesort
Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
5
12
35
42
77 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
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-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
37 23 6 89 15 12 2 19
How to Remember Merge Sort?
That’s 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:
(q,t)
The Siren of Subquadratic Sorts
How To Remember Merge Sort?
Just as Mariah recursively moves
her hands into smaller circles, so
too does merge sort recursively
split an array into smaller
segments.
(q,t)
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)
More TRUTH in CS
• We don’t 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)
LB
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98
Merge
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98
23
Merge
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98
23 98
Merge
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
23 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
23 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
14
Merge
23 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
45
Merge
23 98 14
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
98 45
14
23
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
98 14
14
23 45
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
23 14
14 23
98 45
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
23 98 45
14
14 23 45
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
23 98 45
14
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
23 98 45
14
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6
23 98 45
14
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6
Merge
23 98 45
14
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6
6
Merge
23 98 45
14
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6
67
Merge
23 98 45
14 6
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
23 98 45
14 67
6
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
33
23 98 45
14 67
6
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
42
23 98 45
14 67
6 33
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 6 42
33
14 23 45 98 6
67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 6 33
14 23 45 98 6 33
67 42
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 6 42
33
14 23 45 98 6 33 42
67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 45 98 6 33 42 67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
23 45 98 33 42 67
14 6
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
23 45 98 6 42 67
6
14 33
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 45 98 6 42 67
6 14
23 33
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 98 6 42 67
6 14 23
45 33
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 98 6 33 67
6 14 23 33
45 42
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 98 6 33 42
6 14 23 33 42
45 67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 45 6 33 42
6 14 23 33 42 45
98 67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
23 98 45
14 67
6 42
33
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
67
45
23 14 6 33
98 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?
Bubble Sort.ppt

Mais conteúdo relacionado

Semelhante a Bubble Sort.ppt

Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
 
Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.NikhilSoni177492
 
Bubble Sort presentation.pptx
Bubble Sort presentation.pptxBubble Sort presentation.pptx
Bubble Sort presentation.pptxTusharTikia
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topicSaddam Hussain
 
Heapsortokkay
HeapsortokkayHeapsortokkay
HeapsortokkayRemesha
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Muhammad Hammad Waseem
 
Borrowingwed1.10
Borrowingwed1.10Borrowingwed1.10
Borrowingwed1.10PDS
 
8 elementary sorts-bubble
8 elementary sorts-bubble8 elementary sorts-bubble
8 elementary sorts-bubbleirdginfo
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sortKrish_ver2
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sortNauman Ali
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithmsZaid Hameed
 
Kunci Jawaban kalkulus edisi 9[yunusFairVry.blogspot.com].pdf
Kunci Jawaban kalkulus edisi 9[yunusFairVry.blogspot.com].pdfKunci Jawaban kalkulus edisi 9[yunusFairVry.blogspot.com].pdf
Kunci Jawaban kalkulus edisi 9[yunusFairVry.blogspot.com].pdfMutiaraPutri41
 

Semelhante a Bubble Sort.ppt (20)

Bubble sort
Bubble sortBubble sort
Bubble sort
 
BUBBLESORT
BUBBLESORT BUBBLESORT
BUBBLESORT
 
Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.
 
Bubble Sort presentation.pptx
Bubble Sort presentation.pptxBubble Sort presentation.pptx
Bubble Sort presentation.pptx
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topic
 
Heapsortokkay
HeapsortokkayHeapsortokkay
Heapsortokkay
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
LEC 8-DS ALGO(heaps).pdf
LEC 8-DS  ALGO(heaps).pdfLEC 8-DS  ALGO(heaps).pdf
LEC 8-DS ALGO(heaps).pdf
 
[ACM-ICPC] Sort
[ACM-ICPC] Sort[ACM-ICPC] Sort
[ACM-ICPC] Sort
 
Sorting
SortingSorting
Sorting
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Borrowingwed1.10
Borrowingwed1.10Borrowingwed1.10
Borrowingwed1.10
 
8 elementary sorts-bubble
8 elementary sorts-bubble8 elementary sorts-bubble
8 elementary sorts-bubble
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Notes 6-7
Notes 6-7Notes 6-7
Notes 6-7
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Recursion.pptx
Recursion.pptxRecursion.pptx
Recursion.pptx
 
Kunci Jawaban kalkulus edisi 9[yunusFairVry.blogspot.com].pdf
Kunci Jawaban kalkulus edisi 9[yunusFairVry.blogspot.com].pdfKunci Jawaban kalkulus edisi 9[yunusFairVry.blogspot.com].pdf
Kunci Jawaban kalkulus edisi 9[yunusFairVry.blogspot.com].pdf
 

Mais de MuhammadSheraz836877 (20)

Quick & Merge Sort.ppt
Quick & Merge Sort.pptQuick & Merge Sort.ppt
Quick & Merge Sort.ppt
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
 
CHAPTER 1 BASIC sql STATEMENTS.pptx
CHAPTER 1 BASIC sql STATEMENTS.pptxCHAPTER 1 BASIC sql STATEMENTS.pptx
CHAPTER 1 BASIC sql STATEMENTS.pptx
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
Lecture9_StackQueue.ppt
Lecture9_StackQueue.pptLecture9_StackQueue.ppt
Lecture9_StackQueue.ppt
 
Articles Eng.ppt
Articles Eng.pptArticles Eng.ppt
Articles Eng.ppt
 
FORMS OF MATTER.pptx
FORMS OF MATTER.pptxFORMS OF MATTER.pptx
FORMS OF MATTER.pptx
 
Bonds of solids.pptx
Bonds of solids.pptxBonds of solids.pptx
Bonds of solids.pptx
 
Infinitives_by_M._Sheraz.PPT
Infinitives_by_M._Sheraz.PPTInfinitives_by_M._Sheraz.PPT
Infinitives_by_M._Sheraz.PPT
 
articles.ppt
articles.pptarticles.ppt
articles.ppt
 
Speakingskills. Ppt
Speakingskills. PptSpeakingskills. Ppt
Speakingskills. Ppt
 
speakingskills. Ppt
speakingskills. Pptspeakingskills. Ppt
speakingskills. Ppt
 
voice-techniques. Ppt
voice-techniques. Pptvoice-techniques. Ppt
voice-techniques. Ppt
 
body language. Ppt
body language. Pptbody language. Ppt
body language. Ppt
 
latchesandflipflops.ppt
latchesandflipflops.pptlatchesandflipflops.ppt
latchesandflipflops.ppt
 
listeningskills.ppt
listeningskills.pptlisteningskills.ppt
listeningskills.ppt
 
listeningskills.ppt
listeningskills.pptlisteningskills.ppt
listeningskills.ppt
 
listeningskills.ppt
listeningskills.pptlisteningskills.ppt
listeningskills.ppt
 
Business Econimics.ppt
Business Econimics.pptBusiness Econimics.ppt
Business Econimics.ppt
 
latchesflip-flop DLD
latchesflip-flop DLDlatchesflip-flop DLD
latchesflip-flop DLD
 

Último

Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 

Último (20)

Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

Bubble Sort.ppt

  • 3. Sorting • Sorting takes an unordered collection and makes it an ordered one. 5 12 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 1 2 3 4 5 6
  • 4. "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 5 12 35 42 77 101 1 2 3 4 5 6
  • 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 5 12 35 42 77 101 1 2 3 4 5 6 Swap 42 77
  • 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 5 12 35 77 42 101 1 2 3 4 5 6 Swap 35 77
  • 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 5 12 77 35 42 101 1 2 3 4 5 6 Swap 12 77
  • 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 5 77 12 35 42 101 1 2 3 4 5 6 No need to swap
  • 9. "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 5 77 12 35 42 101 1 2 3 4 5 6 Swap 5 101
  • 10. "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 77 12 35 42 5 1 2 3 4 5 6 101 Largest value correctly placed
  • 11. The “Bubble Up” Algorithm index <- 1 last_compare_at <- n – 1 loop exitif(index > last_compare_at) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop
  • 12. No, Swap isn’t built in. Procedure Swap(a, b isoftype in/out Num) t isoftype Num t <- a a <- b b <- t endprocedure // Swap LB
  • 13. 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 77 12 35 42 5 1 2 3 4 5 6 101 Largest value correctly placed
  • 14. 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.
  • 15. “Bubbling” All the Elements 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101 42 35 12 5 77 1 2 3 4 5 6 101 N - 1
  • 16. Reducing the Number of Comparisons 12 35 42 77 101 1 2 3 4 5 6 5 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101
  • 17. 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 35 12 77 1 2 3 4 5 6 101
  • 18. Putting It All Together
  • 19. N is … // Size of Array Arr_Type definesa Array[1..N] of Num Procedure Swap(n1, n2 isoftype in/out Num) temp isoftype Num temp <- n1 n1 <- n2 n2 <- temp endprocedure // Swap
  • 20. procedure Bubblesort(A isoftype in/out Arr_Type) to_do, index isoftype Num to_do <- N – 1 loop exitif(to_do = 0) index <- 1 loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop to_do <- to_do - 1 endloop endprocedure // Bubblesort Inner loop Outer loop
  • 21. Already Sorted Collections? • What if the collection was already sorted? • What if only a few elements were out of place and after a couple of “bubble ups,” the collection was sorted? • We want to be able to detect this and “stop early”! 42 35 12 5 77 1 2 3 4 5 6 101
  • 22. Using a Boolean “Flag” • We can use a boolean variable to determine if any swapping occurred during the “bubble up.” • If no swapping occurred, then we know that the collection is already sorted! • This boolean “flag” needs to be reset after each “bubble up.”
  • 23. did_swap isoftype Boolean did_swap <- true loop exitif ((to_do = 0) OR NOT(did_swap)) index <- 1 did_swap <- false loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) did_swap <- true endif index <- index + 1 endloop to_do <- to_do - 1 endloop
  • 24. An Animated Example 67 45 23 14 6 33 98 42 1 2 3 4 5 6 7 8 to_do index 7 N 8 did_swap true
  • 25. An Animated Example 67 45 23 14 6 33 98 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 did_swap false
  • 26. An Animated Example 67 45 23 14 6 33 98 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap false
  • 27. An Animated Example 67 45 98 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap true
  • 28. An Animated Example 67 45 98 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 did_swap true
  • 29. An Animated Example 67 45 98 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true
  • 30. An Animated Example 67 98 45 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true
  • 31. An Animated Example 67 98 45 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 did_swap true
  • 32. An Animated Example 67 98 45 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true
  • 33. An Animated Example 67 14 45 98 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true
  • 34. An Animated Example 67 14 45 98 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 did_swap true
  • 35. An Animated Example 67 14 45 98 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true
  • 36. An Animated Example 67 14 45 6 98 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true
  • 37. An Animated Example 67 14 45 6 98 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 did_swap true
  • 38. An Animated Example 67 14 45 6 98 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true
  • 39. An Animated Example 98 14 45 6 67 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true
  • 40. An Animated Example 98 14 45 6 67 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 did_swap true
  • 41. An Animated Example 98 14 45 6 67 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true
  • 42. An Animated Example 33 14 45 6 67 98 23 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true
  • 43. An Animated Example 33 14 45 6 67 98 23 42 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 did_swap true
  • 44. An Animated Example 33 14 45 6 67 98 23 42 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 Swap did_swap true
  • 45. An Animated Example 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 Swap did_swap true
  • 46. After First Pass of Outer Loop 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 7 8 N 8 Finished first “Bubble Up” did_swap true
  • 47. The Second “Bubble Up” 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 1 N 8 did_swap false
  • 48. The Second “Bubble Up” 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 1 N 8 did_swap false No Swap
  • 49. The Second “Bubble Up” 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap false
  • 50. The Second “Bubble Up” 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap false Swap
  • 51. The Second “Bubble Up” 33 45 14 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap true Swap
  • 52. The Second “Bubble Up” 33 45 14 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true
  • 53. The Second “Bubble Up” 33 45 14 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true Swap
  • 54. The Second “Bubble Up” 33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true Swap
  • 55. The Second “Bubble Up” 33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 4 N 8 did_swap true
  • 56. The Second “Bubble Up” 33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 4 N 8 did_swap true No Swap
  • 57. The Second “Bubble Up” 33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true
  • 58. The Second “Bubble Up” 33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true Swap
  • 59. The Second “Bubble Up” 67 6 14 45 33 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true Swap
  • 60. The Second “Bubble Up” 67 6 14 45 33 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true
  • 61. The Second “Bubble Up” 67 6 14 45 33 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true Swap
  • 62. The Second “Bubble Up” 42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true Swap
  • 63. After Second Pass of Outer Loop 42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 6 7 N 8 did_swap true Finished second “Bubble Up”
  • 64. The Third “Bubble Up” 42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap false
  • 65. The Third “Bubble Up” 42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap false Swap
  • 66. The Third “Bubble Up” 42 6 23 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap true Swap
  • 67. The Third “Bubble Up” 42 6 23 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true
  • 68. The Third “Bubble Up” 42 6 23 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true Swap
  • 69. The Third “Bubble Up” 42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true Swap
  • 70. The Third “Bubble Up” 42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 3 N 8 did_swap true
  • 71. The Third “Bubble Up” 42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 3 N 8 did_swap true No Swap
  • 72. The Third “Bubble Up” 42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true
  • 73. The Third “Bubble Up” 42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true Swap
  • 74. The Third “Bubble Up” 42 23 6 33 45 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true Swap
  • 75. The Third “Bubble Up” 42 23 6 33 45 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true
  • 76. The Third “Bubble Up” 42 23 6 33 45 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true Swap
  • 77. The Third “Bubble Up” 45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true Swap
  • 78. After Third Pass of Outer Loop 45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 6 N 8 did_swap true Finished third “Bubble Up”
  • 79. The Fourth “Bubble Up” 45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap false
  • 80. The Fourth “Bubble Up” 45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap false Swap
  • 81. The Fourth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap true Swap
  • 82. The Fourth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 2 N 8 did_swap true
  • 83. The Fourth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 2 N 8 did_swap true No Swap
  • 84. The Fourth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 3 N 8 did_swap true
  • 85. The Fourth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 3 N 8 did_swap true No Swap
  • 86. The Fourth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 4 N 8 did_swap true
  • 87. The Fourth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 4 N 8 did_swap true No Swap
  • 88. After Fourth Pass of Outer Loop 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 5 N 8 did_swap true Finished fourth “Bubble Up”
  • 89. The Fifth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 1 N 8 did_swap false
  • 90. The Fifth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 1 N 8 did_swap false No Swap
  • 91. The Fifth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 2 N 8 did_swap false
  • 92. The Fifth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 2 N 8 did_swap false No Swap
  • 93. The Fifth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 3 N 8 did_swap false
  • 94. The Fifth “Bubble Up” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 3 N 8 did_swap false No Swap
  • 95. After Fifth Pass of Outer Loop 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 4 N 8 did_swap false Finished fifth “Bubble Up”
  • 96. Finished “Early” 45 23 14 33 42 67 6 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 elements must be correctly placed. We can “skip” the last two passes of the outer loop.
  • 97. 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
  • 98. Truth in CS Act • NOBODY EVER USES BUBBLE SORT • NOBODY • NOT EVER • BECAUSE IT IS EXTREMELY INEFFICIENT LB
  • 101. Sorting • Sorting takes an unordered collection and makes it an ordered one. 5 12 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 1 2 3 4 5 6
  • 102. 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
  • 103. 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 37 23 6 89 15 12 2 19
  • 104. How to Remember Merge Sort? That’s 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: (q,t) The Siren of Subquadratic Sorts
  • 105. How To Remember Merge Sort? Just as Mariah recursively moves her hands into smaller circles, so too does merge sort recursively split an array into smaller segments. (q,t) We need two such recursions, one for each half of the split array.
  • 106. 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)
  • 107. More TRUTH in CS • We don’t 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
  • 108. 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
  • 109. 67 45 23 14 6 33 98 42
  • 110. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42
  • 111. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98
  • 112. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98
  • 113. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 Merge
  • 114. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 23 Merge
  • 115. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 23 98 Merge
  • 116. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 23 98
  • 117. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 98
  • 118. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 14 Merge 23 98
  • 119. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 45 Merge 23 98 14
  • 120. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 98 45 14 23
  • 121. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 98 14 14 23 45
  • 122. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 14 14 23 98 45
  • 123. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 98 45 14 14 23 45
  • 124. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 98 45 14 14 23 45 98
  • 125. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 23 98 45 14 14 23 45 98
  • 126. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 23 98 45 14 14 23 45 98
  • 127. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 Merge 23 98 45 14 14 23 45 98
  • 128. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 6 Merge 23 98 45 14 14 23 45 98
  • 129. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 67 Merge 23 98 45 14 6 14 23 45 98
  • 130. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 23 98 45 14 67 6 14 23 45 98
  • 131. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 14 23 45 98
  • 132. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 33 23 98 45 14 67 6 14 23 45 98
  • 133. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 42 23 98 45 14 67 6 33 14 23 45 98
  • 134. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 45 98
  • 135. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 6 42 33 14 23 45 98 6 67
  • 136. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 6 33 14 23 45 98 6 33 67 42
  • 137. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 6 42 33 14 23 45 98 6 33 42 67
  • 138. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 45 98 6 33 42 67
  • 139. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 23 45 98 33 42 67 14 6
  • 140. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 23 45 98 6 42 67 6 14 33
  • 141. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 45 98 6 42 67 6 14 23 33
  • 142. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 98 6 42 67 6 14 23 45 33
  • 143. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 98 6 33 67 6 14 23 33 45 42
  • 144. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 98 6 33 42 6 14 23 33 42 45 67
  • 145. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 45 6 33 42 6 14 23 33 42 45 98 67
  • 146. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67
  • 147. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98
  • 148. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 23 98 45 14 67 6 42 33 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98
  • 149. 67 45 23 14 6 33 98 42 6 14 23 33 42 45 67 98
  • 150. Summary • Divide the unsorted collection into two • Until the sub-arrays only contain one element • Then merge the sub-problem solutions together