dale roberts mergesort dale roberts, lecturer computer science, iupui e-mail: [email protected]...
TRANSCRIPT
Dale Roberts
MergesortMergesort
Dale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected]
Department of Computer and Information Science,School of Science, IUPUI
Dale Roberts
Why Does It Matter?Why Does It Matter?
1000
Time tosolve a
problemof size
10,000
100,000
million
10 million
1.3 seconds
22 minutes
15 days
41 years
41 millennia
920
3,600
14,000
41,000
1,000
Run time(nanoseconds) 1.3 N3
secondMax sizeproblemsolvedin one
minute
hour
day
10 msec
1 second
1.7 minutes
2.8 hours
1.7 weeks
10,000
77,000
600,000
2.9 million
100
10 N2
0.4 msec
6 msec
78 msec
0.94 seconds
11 seconds
1 million
49 million
2.4 trillion
50 trillion
10+
47 N log2N
0.048 msec
0.48 msec
4.8 msec
48 msec
0.48 seconds
21 million
1.3 billion
76 trillion
1,800 trillion
10
48 N
N multiplied by 10,time multiplied by
Dale Roberts
Orders of MagnitudeOrders of Magnitude
10-10
Meters PerSecond
10-8
10-6
10-4
10-2
1
102
1.2 in / decade
ImperialUnits
1 ft / year
3.4 in / day
1.2 ft / hour
2 ft / minute
2.2 mi / hour
220 mi / hour
Continental drift
Example
Hair growing
Glacier
Gastro-intestinal tract
Ant
Human walk
Propeller airplane
104
106
108
370 mi / min
620 mi / sec
62,000 mi / sec
Space shuttle
Earth in galactic orbit
1/3 speed of light
1
Seconds
102
103
104
105
106
107
108
109
1010
1 second
Equivalent
1.7 minutes
17 minutes
2.8 hours
1.1 days
1.6 weeks
3.8 months
3.1 years
3.1 decades
3.1 centuries
forever
1021 age ofuniverse
210 thousand
220 million
230 billion
. . .
10 10 seconds
Powersof 2
Dale Roberts
Impact of Better AlgorithmsImpact of Better Algorithms
Example 1: N-body-simulation.Example 1: N-body-simulation.Simulate gravitational interactions among N bodies.Simulate gravitational interactions among N bodies.
physicists want N = # atoms in universephysicists want N = # atoms in universe
Brute force method: NBrute force method: N22 steps. steps.Appel (1981). Appel (1981). N log N steps, enables new research. N log N steps, enables new research.
Example 2: Discrete Fourier Transform (DFT).Example 2: Discrete Fourier Transform (DFT).Breaks down waveforms (sound) into periodic components.Breaks down waveforms (sound) into periodic components.
foundation of signal processingfoundation of signal processingCD players, JPEG, analyzing astronomical data, etc.CD players, JPEG, analyzing astronomical data, etc.
Grade school method: NGrade school method: N22 steps. steps.Runge-König (1924), Cooley-Tukey (1965).Runge-König (1924), Cooley-Tukey (1965).FFT algorithm: N log N steps, enables new technology.FFT algorithm: N log N steps, enables new technology.
Dale Roberts
MergesortMergesort
Mergesort (divide-and-conquer)Mergesort (divide-and-conquer)Divide array into two halves.Divide array into two halves.
A L G O R I T H M S
divideA L G O R I T H M S
Dale Roberts
MergesortMergesort
Mergesort (divide-and-conquer)Mergesort (divide-and-conquer)Divide array into two halves.Divide array into two halves.
Recursively sort each half.Recursively sort each half.
sort
A L G O R I T H M S
divideA L G O R I T H M S
A G L O R H I M S T
Dale Roberts
MergesortMergesort
Mergesort (divide-and-conquer)Mergesort (divide-and-conquer)Divide array into two halves.Divide array into two halves.
Recursively sort each half.Recursively sort each half.
Merge two halves to make sorted whole.Merge two halves to make sorted whole.
merge
sort
A L G O R I T H M S
divideA L G O R I T H M S
A G L O R H I M S T
A G H I L M O R S T
Dale Roberts
auxiliary array
smallest smallest
A G L O R H I M S T
MergingMerging
Merge.Merge.Keep track of smallest element in each sorted half.Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.Insert smallest of two elements into auxiliary array.
Repeat until done.Repeat until done.
A
Dale Roberts
auxiliary array
smallest smallest
A G L O R H I M S T
A
MergingMerging
Merge.Merge.Keep track of smallest element in each sorted half.Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.Insert smallest of two elements into auxiliary array.
Repeat until done.Repeat until done.
G
Dale Roberts
auxiliary array
smallest smallest
A G L O R H I M S T
A G
MergingMerging
Merge.Merge.Keep track of smallest element in each sorted half.Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.Insert smallest of two elements into auxiliary array.
Repeat until done.Repeat until done.
H
Dale Roberts
auxiliary array
smallest smallest
A G L O R H I M S T
A G H
MergingMerging
Merge.Merge.Keep track of smallest element in each sorted half.Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.Insert smallest of two elements into auxiliary array.
Repeat until done.Repeat until done.
I
Dale Roberts
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I
MergingMerging
Merge.Merge.Keep track of smallest element in each sorted half.Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.Insert smallest of two elements into auxiliary array.
Repeat until done.Repeat until done.
L
Dale Roberts
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L
MergingMerging
Merge.Merge.Keep track of smallest element in each sorted half.Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.Insert smallest of two elements into auxiliary array.
Repeat until done.Repeat until done.
M
Dale Roberts
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M
MergingMerging
Merge.Merge.Keep track of smallest element in each sorted half.Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.Insert smallest of two elements into auxiliary array.
Repeat until done.Repeat until done.
O
Dale Roberts
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M O
MergingMerging
Merge.Merge.Keep track of smallest element in each sorted half.Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.Insert smallest of two elements into auxiliary array.
Repeat until done.Repeat until done.
R
Dale Roberts
auxiliary array
first halfexhausted smallest
A G L O R H I M S T
A G H I L M O R
MergingMerging
Merge.Merge.Keep track of smallest element in each sorted half.Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.Insert smallest of two elements into auxiliary array.
Repeat until done.Repeat until done.
S
Dale Roberts
auxiliary array
first halfexhausted smallest
A G L O R H I M S T
A G H I L M O R S
MergingMerging
Merge.Merge.Keep track of smallest element in each sorted half.Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.Insert smallest of two elements into auxiliary array.
Repeat until done.Repeat until done.
T
Dale Roberts
Implementing MergesortImplementing Mergesort
Item aux[MAXN];
void mergesort(Item a[], int left, int right) { int mid = (right + left) / 2; if (right <= left) return; mergesort(a, left, mid); mergesort(a, mid + 1, right); merge(a, left, mid, right);}
mergesort (see Sedgewick Program 8.3)
uses scratch array
Dale Roberts
Implementing Merge (Idea 0)Implementing Merge (Idea 0)mergeAB(Item c[], Item a[], int N, Item b[], int M )mergeAB(Item c[], Item a[], int N, Item b[], int M ) { int i, j, k;{ int i, j, k; for (i = 0, j = 0, k = 0; k < N+M; k++)for (i = 0, j = 0, k = 0; k < N+M; k++) {{ if (i == N) { c[k] = b[j++]; continue; }if (i == N) { c[k] = b[j++]; continue; } if (j == M) { c[k] = a[i++]; continue; }if (j == M) { c[k] = a[i++]; continue; } c[k] = (less(a[i], b[j])) ? a[i++] : b[j++];c[k] = (less(a[i], b[j])) ? a[i++] : b[j++]; }} }}
Dale Roberts
Implementing MergesortImplementing Mergesort
void merge(Item a[], int left, int mid, int right) { int i, j, k; for (i = mid+1; i > left; i--) aux[i-1] = a[i-1]; for (j = mid; j < right; j++) aux[right+mid-j] = a[j+1];
for (k = left; k <= right; k++) if (ITEMless(aux[i], aux[j])) a[k] = aux[i++]; else a[k] = aux[j--];}
merge (see Sedgewick Program 8.2)
copy to temporary array
merge two sorted sequences
Dale Roberts
Mergesort DemoMergesort Demo
Mergesort Mergesort The auxilliary array used in the merging The auxilliary array used in the merging operation is shown to the right of the array a[], going operation is shown to the right of the array a[], going from (N+1, 1) to (2N, 2N). from (N+1, 1) to (2N, 2N).
The demo is a dynamic representation of the algorithm The demo is a dynamic representation of the algorithm in action, sorting an array a containing a permutation of in action, sorting an array a containing a permutation of the integers 1 through N. For each i, the array element the integers 1 through N. For each i, the array element a[i] is depicted as a black dot plotted at position (i, a[i]). a[i] is depicted as a black dot plotted at position (i, a[i]). Thus, the end result of each sort is a diagonal of black Thus, the end result of each sort is a diagonal of black dots going from (1, 1) at the bottom left to (N, N) at the dots going from (1, 1) at the bottom left to (N, N) at the top right. Each time an element is moved, a green dot is top right. Each time an element is moved, a green dot is left at its old position. Thus the moving black dots give a left at its old position. Thus the moving black dots give a dynamic representation of the progress of the sort and dynamic representation of the progress of the sort and the green dots give a history of the data-movement cost. the green dots give a history of the data-movement cost.