Insertion Sort
●
while some elements unsorted:
●
Using linear search, find the location in the sorted portion
where the 1
stelement of the unsorted portion should be
inserted
●
Move all the elements after the insertion location up one
position to make space for the new element
13 21 45 79 47 22 38 60 66 74 36 94 57 16 29 81 45 66 60 45
Insertion Sort Algorithm
public void insertionSort(Comparable[] arr) {
for (int i = 1; i < arr.length; ++i) { Comparable temp = arr[i];
int pos = i;
// Shuffle up all sorted items > arr[i]
while (pos > 0 &&
arr[pos-1].compareTo(temp) > 0) { arr[pos] = arr[pos–1];
pos--;
} // end while
// Insert the current item arr[pos] = temp;
} }
public void insertionSort(Comparable[] arr) {
for (int i = 1; i < arr.length; ++i) { Comparable temp = arr[i];
int pos = i;
// Shuffle up all sorted items > arr[i]
while (pos > 0 &&
arr[pos-1].compareTo(temp) > 0) { arr[pos] = arr[pos–1];
pos--;
} // end while
// Insert the current item arr[pos] = temp;
} }
Insertion Sort Analysis
outer loop outer times
inner loop
Insertion Sort: Number of
Comparisons
# of Sorted
Elements
Best case
Worst case
0
0
0
1
1
1
2
1
2
…
…
…
n-1
1
n-1
n-1
n(n-1)/2
Insertion Sort: Cost Function
●
1 operation to initialize the outer loop
●The outer loop is evaluated n-1 times
● 5 instructions (including outer loop comparison and increment) ● Total cost of the outer loop: 5(n-1)
●
How many times the inner loop is evaluated is affected by the
state of the array to be sorted
●
Best case: the array is already completely sorted so no “shifting”
of array elements is required.
● We only test the condition of the inner loop once (2 operations = 1
comparison + 1 element comparison), and the body is never executed
Insertion Sort: Cost Function
●
Worst case: the array is sorted in reverse order (so each item
has to be moved to the front of the array)
● In the i-th iteration of the outer loop, the inner loop will perform 4i+1
operations
● Therefore, the total cost of the inner loop will be 2n(n-1)+n-1
●
Time cost:
● Best case: 7(n-1)
● Worst case: 5(n-1)+2n(n-1)+n-1
●
What about the number of moves?
● Best case: 2(n-1) moves ● Worst case: 2(n-1)+n(n-1)/2
Insertion Sort: Average Case
●
Is it closer to the best case (n comparisons)?
●The worst case (n * (n-1) / 2) comparisons?
●
It turns out that when random data is sorted, insertion sort is
usually closer to the worst case
● Around n * (n-1) / 4 comparisons● Calculating the average number of comparisons more exactly would
require us to state assumptions about what the “average” input data set looked like
● This would, for example, necessitate discussion of how items were
distributed over the array
●
Exact calculation of the number of operations required to perform
even simple algorithms can be challenging
(for instance, assume that each initial order of elements has the
same probability to occur)
Selection Sort
5
1
3
4
6
2
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
6
2
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
6
2
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
6
2
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
6
2
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
6
2
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
6
2
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
6
2
Comparison Data Movement Sorted LargestSelection Sort
5
1
3
4
2
6
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
2
6
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
2
6
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
2
6
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
2
6
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
2
6
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
2
6
Comparison Data Movement Sorted
Selection Sort
5
1
3
4
2
6
Comparison Data Movement Sorted LargestSelection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted LargestSelection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted LargestSelection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
2
1
3
4
5
6
Comparison Data Movement Sorted LargestSelection Sort
1
2
3
4
5
6
Comparison Data Movement Sorted
Selection Sort
1
2
3
4
5
6
Comparison Data Movement SortedDONE!
void selection_sort(int n, int list[])
{
int min, temp;
int k, l, index;
for(index = 0; index< n - 1 ; index++)
{
min = index ;
for(k = index + 1; k < n ; k ++)
{
if(list[min] > list[k])
min = k ;
}
if(min != index)
{
temp = `list [index];
list[index] = list[min];
list[min] = temp;
}
printf(" \n Step : %d :",(index+1));
for( l = 0 ; l < n; l++)
printf(" %d", list[l]);
}
printf("\n Sorted list is as follows:\n");
for( index = 0 ; index < n; index++)
printf(" %d", list[index]);
return 0;
}
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
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
LBItems 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
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
“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 - 1Reducing 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 101Reducing the Number of
Comparisons
●
On the N
th
“bubble up”, we only need to
do
MAX-N comparisons
.
●
For example:
●
This is the 4
th“bubble up”
●MAX is 6
●
Thus we have
2 comparisons
to do
42 5 35 12 77 1 2 3 4 5 6 101
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
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
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
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 trueAn 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 falseAn 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 falseAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAn 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 trueAfter 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 8Finished first “Bubble Up”
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 falseThe 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 SwapThe 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 falseThe 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 SwapThe 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 SwapThe 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 trueThe 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 SwapThe 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 SwapThe 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 trueThe 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 SwapThe 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 trueThe 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 SwapThe 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 SwapThe 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 trueThe 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 SwapThe 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 SwapAfter 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 trueThe 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 falseThe 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 SwapThe 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 SwapThe 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 trueThe 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 SwapThe 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 SwapThe 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 trueThe 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 SwapThe 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 trueThe 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 SwapThe 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 SwapThe 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 trueThe 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 SwapThe 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 SwapAfter 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 trueThe 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 falseThe 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 SwapThe 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 SwapThe 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 trueThe 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 SwapThe 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 trueThe 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 SwapThe 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 trueThe 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 SwapAfter 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 trueThe 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 falseThe 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 SwapThe 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 falseThe 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 SwapThe 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 falseThe 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 SwapAfter 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 falseFinished “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
Truth in CS Act
●
NOBODY EVER USES BUBBLE SORT
●
NOBODY
●
NOT EVER
●
BECAUSE IT IS EXTREMELY INEFFICIENT
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
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
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:
ƒ
(
θ,t)
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.
ƒ
(
θ,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
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 f2Algorithm
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)
67 45
23 14 6 33
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 14 45 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 14 45 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 14 45 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 14 45 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 14 45 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 14 45 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 14 45 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 14 45 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 14 45 6 67 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 14 45 6 67 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 14 45 6 67 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 14 45 6 67 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 14 45 6 67 33 42 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 14 45 6 33 42 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 14 45 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 14 45 6 33 42 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 14 45 6 67 33 42 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 14 45 6 67 33 42 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 14 45 6 67 33 42 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 14 45 6 67 33 42 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 14 45 6 67 33 42 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 14 45 6 67 33 42 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 14 45 6 67 33 42 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 14 45 6 67 33 42 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 14 45 6 67 33 42 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 14 45 6 67 33 42 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 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98
67 45
23 14 6 33
98 42
Summary
●
Divide
the unsorted collection
into two
●
Until the sub-arrays only
contain one element
●
Then
merge the sub-problem solutions
O-notation Introduction
●
Exact counting of operations is often difficult (and
tedious), even for simple algorithms
●
Often, exact counts are not useful due to other
factors, e.g. the language/machine used, or the
implementation of the algorithm (different types of
operations do not take the same time anyway)
●
O-notation is a mathematical language for
evaluating the running-time (and memory usage) of
algorithms
Growth Rate of an Algorithm
●
We often want to compare the performance of
algorithms
●
When doing so we generally want to know how they
perform when the problem size (n) is large
●
Since cost functions are complex, and may be
difficult to compute, we approximate them using O
notation
Example of a Cost Function
●
Cost Function: t
A
(n) = n
2
+ 20n + 100
●
Which term dominates?
●
It depends on the size of n
●
n = 2, t
A
(n) = 4 + 40 + 100
●
The constant, 100, is the dominating term
●n = 10, t
A
(n) = 100 + 200 + 100
●
20n is the dominating term
●n = 100, t
A
(n) = 10,000 + 2,000 + 100
●
n
2is the dominating term
●
n = 1000, t
A