mergesort and quicksort - ucozramzi.ucoz.com/datastruandalg/lec5-mergesort.pdf2 two classic sorting...
TRANSCRIPT
![Page 1: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/1.jpg)
http:/ /algs4.cs.princeton.edu
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE
http:/ /algs4.cs.princeton.edu
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE
MERGESORT
Modified by: Dr. Fahed Jubair and Dr. Ramzi Saifan
Computer Engineering Department
University of Jordan
![Page 2: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/2.jpg)
2
Two classic sorting algorithms: mergesort and quicksort
Critical components in the world’s computational infrastructure.
・Full scientific understanding of their properties has enabled us
to develop them into practical system sorts.
・Quicksort honored as one of top 10 algorithms of 20th
century
in science and engineering.
Mergesort. [this lecture]
Quicksort. [next lecture]
...
...
![Page 3: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/3.jpg)
http:/ /algs4.cs.princeton.eduhttp:/ /algs4.cs.princeton.edu
‣ mergesort
MERGESORT
![Page 4: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/4.jpg)
Basic plan.
・Divide array into two halves.
・Recursively sort each half.
・Merge two halves.
4
Mergesort
![Page 5: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/5.jpg)
5
Mergesort: Java implementation
lo mid hi
10 11 12 13 14 15 16 17 18 19
public class Merge
{
private static void merge(...)
{ /* as before */ }
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
{
if (hi <= lo) return;
int mid = lo + (hi - lo) / 2;
sort(a, aux, lo, mid);
sort(a, aux, mid+1, hi);
merge(a, aux, lo, mid, hi);
}
public static void sort(Comparable[] a)
{
Comparable[] aux = new Comparable[a.length];
sort(a, aux, 0, a.length - 1);
}
}
![Page 6: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/6.jpg)
6
Merging: Java implementation
A G L O R H I M S T
A G H I L M
i j
k
lo himid
aux[]
a[]
private static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi)
{
for (int k = lo; k <= hi; k++)
aux[k] = a[k];
int i = lo, j = mid+1;
for (int k = lo; k <= hi; k++)
{
if (i > mid) a[k] = aux[j++];
else if (j > hi) a[k] = aux[i++];
else if (less(aux[j], aux[i])) a[k] = aux[j++];
else a[k] = aux[i++];
}
}
copy
merge
![Page 7: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/7.jpg)
7
Abstract in-place merge demo
E E G M R A C E R T
lo mid mid+1 hi
a[]
sorted sorted
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 8: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/8.jpg)
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
Merging demo
E E G M R A C E R T
lo mid mid+1 hi
a[]
copy to auxiliary array
aux[]
E E G M R A C E R T
![Page 9: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/9.jpg)
Merging demo
E E G M R A C E R Taux[]
a[] E E G M R A C E R T
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 10: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/10.jpg)
Merging demo
compare minimum in each subarray
E E G M R A C E R Taux[]
i j
a[] E E G M R A C E R T
k
A
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 11: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/11.jpg)
Merging demo
compare minimum in each subarray
E E G M R A C E R Taux[]
i j
a[] E E G M R A C E R T
k
A
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 12: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/12.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A E G M R A C E R Ta[]
k
C
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
compare minimum in each subarray
![Page 13: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/13.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A E G M R A C E R Ta[]
k
compare minimum in each subarray
C
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 14: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/14.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C G M R A C E R Ta[]
k
E
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
compare minimum in each subarray
![Page 15: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/15.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C G M R A C E R Ta[]
k
compare minimum in each subarray
E
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 16: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/16.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E M R A C E R Ta[]
k
E
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
compare minimum in each subarray
![Page 17: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/17.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E M R A C E R Ta[]
k
compare minimum in each subarray
E
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 18: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/18.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E R A C E R Ta[]
k
E
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
compare minimum in each subarray
![Page 19: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/19.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E R A C E R Ta[]
k
compare minimum in each subarray
E
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 20: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/20.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E E A C E R Ta[]
k
G
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
compare minimum in each subarray
![Page 21: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/21.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E E A C E R Ta[]
k
compare minimum in each subarray
G
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 22: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/22.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E E G C E R Ta[]
k
M
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
compare minimum in each subarray
![Page 23: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/23.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E E G C E R Ta[]
k
compare minimum in each subarray
M
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 24: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/24.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E E G M E R Ta[]
k
R
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
compare minimum in each subarray
![Page 25: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/25.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E E G M E R Ta[]
k
compare minimum in each subarray
R
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 26: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/26.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E E G M R R Ta[]
k
one subarray exhausted, take from other
R
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 27: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/27.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E E G M R R Ta[]
k
one subarray exhausted, take from other
R
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 28: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/28.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E E G M R R Ta[]
k
T
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
one subarray exhausted, take from other
![Page 29: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/29.jpg)
Merging demo
E E G M R A C E R Taux[]
i j
A C E E E G M R R Ta[]
k
one subarray exhausted, take from other
T
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 30: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/30.jpg)
Merging demo
E E G M R A C E R T
both subarrays exhausted, done
A C E E E G M R R Ta[]
aux[]
i j
k
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 31: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/31.jpg)
31
Abstract in-place merge demo
A C E E E G M R R Ta[]
sorted
lo hi
Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi],
replace with sorted subarray a[lo] to a[hi].
![Page 32: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/32.jpg)
33
Merging: Java implementation
A G L O R H I M S T
A G H I L M
i j
k
lo himid
aux[]
a[]
private static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi)
{
for (int k = lo; k <= hi; k++)
aux[k] = a[k];
int i = lo, j = mid+1;
for (int k = lo; k <= hi; k++)
{
if (i > mid) a[k] = aux[j++];
else if (j > hi) a[k] = aux[i++];
else if (less(aux[j], aux[i])) a[k] = aux[j++];
else a[k] = aux[i++];
}
}
copy
merge
![Page 33: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/33.jpg)
Mergesort: animation
36
http://www.sorting-algorithms.com/merge-sort
50 random items
in order
current subarray
algorithm position
not in order
![Page 34: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/34.jpg)
Mergesort: animation
37
http://www.sorting-algorithms.com/merge-sort
50 reverse-sorted items
in order
current subarray
algorithm position
not in order
![Page 35: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/35.jpg)
Proposition. Mergesort uses ≤ N lg N compares to sort an array of length N.
Pf sketch. The number of compares C (N) to mergesort an array of length N
satisfies the recurrence:
C (N) C (N / 2) + C (N / 2) + N for N > 1, with C (1) = 0.
We solve the recurrence when N is a power of 2:
D (N) = 2 D (N / 2) + N, for N > 1, with D (1) = 0.
38
Mergesort: number of compares
left half right half merge
result holds for all N
(analysis cleaner in this case)
![Page 36: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/36.jpg)
Proposition. If D (N) satisfies D (N) = 2 D (N / 2) + N for N > 1, with D (1) = 0,
then D (N) = N lg N.
Pf 1. [assuming N is a power of 2]
39
Divide-and-conquer recurrence: proof by picture
lg N
T(N) = N lg N
N = N
2 (N/2) = N
8 (N/8) = N
⋮
D (N)
4 (N/4) = N
D (N / 2) D (N / 2)
D(N / 8) D(N / 8)D(N / 8) D(N / 8) D(N / 8) D(N / 8)D(N / 8) D(N / 8)
D(N / 4) D(N / 4) D(N / 4) D(N / 4)
⋮
![Page 37: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/37.jpg)
42
Mergesort: empirical analysis
Running time estimates:
・Laptop executes 108
compares/second.
・Supercomputer executes 1012
compares/second.
Bottom line. Good algorithms are better than supercomputers.
insertion sort (N2) mergesort (N log N)
computer thousand million billion thousand million billion
home instant 2.8 hours 317 years instant 1 second 18 min
super instant 1 second 1 week instant instant instant
![Page 38: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/38.jpg)
Proposition. Mergesort uses ≤ 6 N lg N array accesses to sort an array of
length N.
Pf sketch. The number of array accesses A (N) satisfies the recurrence:
A (N) A (N / 2) + A (N / 2) + 6 N for N > 1, with A (1) = 0.
Key point. Any algorithm with the following structure takes N log N time:
Notable examples. FFT, hidden-line removal, Kendall-tau distance, …
43
Mergesort: number of array accesses
public static void linearithmic(int N)
{
if (N == 0) return;
linearithmic(N/2);
linearithmic(N/2);
linear(N);
}
solve two problems
of half the size
do a linear amount of work
![Page 39: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/39.jpg)
44
Mergesort analysis: memory
Proposition. Mergesort uses extra space proportional to N.
Pf. The array aux[] needs to be of length N for the last merge.
Def. A sorting algorithm is in-place if it uses c log N extra memory.
Ex. Insertion sort, selection sort.
Challenge 1 (not hard). Use aux[] array of length ~ ½ N instead of N.
Challenge 2 (very hard). In-place merge. [Kronrod 1969]
A C D G H I M N U V
A B C D E F G H I J M N O P Q R S T U V
B E F J O P Q R S T
two sorted subarrays
merged result
![Page 40: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/40.jpg)
45
Mergesort: practical improvements
Use insertion sort for small subarrays.
・Mergesort has too much overhead for tiny subarrays.
・Cutoff to insertion sort for 10 items.
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
{
if (hi <= lo + CUTOFF - 1)
{
Insertion.sort(a, lo, hi);
return;
}
int mid = lo + (hi - lo) / 2;
sort (a, aux, lo, mid);
sort (a, aux, mid+1, hi);
merge(a, aux, lo, mid, hi);
}
![Page 41: Mergesort and quicksort - uCozramzi.ucoz.com/DataStruAndAlg/lec5-Mergesort.pdf2 Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational](https://reader034.vdocuments.net/reader034/viewer/2022042711/5f80c49dee70917cb867ae53/html5/thumbnails/41.jpg)
47
Mergesort: practical improvements
Stop if already sorted.
・Is largest item in first half ≤ smallest item in second half?
・Helps for partially-ordered arrays.
A B C D E F G H I J
A B C D E F G H I J M N O P Q R S T U V
M N O P Q R S T U V
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
{
if (hi <= lo) return;
int mid = lo + (hi - lo) / 2;
sort (a, aux, lo, mid);
sort (a, aux, mid+1, hi);
if (!less(a[mid+1], a[mid])) return;
merge(a, aux, lo, mid, hi);
}