Professional Documents
Culture Documents
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
Algorithms
comparators
F O U R T H E D I T I O N
...
...
2
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
Algorithms
comparators
Basic plan.
Divide array into two halves.
Recursively sort each half.
Merge two halves.
input M E R G E S O R T E X A M P L E
sort left half E E G M O R R S T E X A M P L E
sort right half E E G M O R R S A E E L M P T X
merge results A E E E E G L M M O P R R S T X
Mergesort overview
4
Abstract in-place merge demo
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].
lo mid mid+1 hi
a[] E E G M R A C E R T
sorted sorted
5
Abstract in-place merge demo
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].
lo hi
a[] A C E E E G M R R T
sorted
6
Merging: Java implementation
private static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi)
{
lo i mid j hi
aux[] A G L O R H I M S T
k
a[] A G H I L M
7
Mergesort: Java implementation
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);
}
lo mid hi
10 11 12 13 14 15 16 17 18 19
8
Mergesort: trace
a[]
lo hi 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
M E R G E S O R T E X A M P L E
merge(a, aux, 0, 0, 1) E M R G E S O R T E X A M P L E
merge(a, aux, 2, 2, 3) E M G R E S O R T E X A M P L E
merge(a, aux, 0, 1, 3) E G M R E S O R T E X A M P L E
merge(a, aux, 4, 4, 5) E G M R E S O R T E X A M P L E
merge(a, aux, 6, 6, 7) E G M R E S O R T E X A M P L E
merge(a, aux, 4, 5, 7) E G M R E O R S T E X A M P L E
merge(a, aux, 0, 3, 7) E E G M O R R S T E X A M P L E
merge(a, aux, 8, 8, 9) E E G M O R R S E T X A M P L E
merge(a, aux, 10, 10, 11) E E G M O R R S E T A X M P L E
merge(a, aux, 8, 9, 11) E E G M O R R S A E T X M P L E
merge(a, aux, 12, 12, 13) E E G M O R R S A E T X M P L E
merge(a, aux, 14, 14, 15) E E G M O R R S A E T X M P E L
merge(a, aux, 12, 13, 15) E E G M O R R S A E T X E L M P
merge(a, aux, 8, 11, 15) E E G M O R R S A E E L M P T X
merge(a, aux, 0, 7, 15) A E E E E G L M M O P R R S T X
Trace of merge results for top-down mergesort
9
Mergesort: animation
50 random items
algorithm position
in order
current subarray
not in order
http://www.sorting-algorithms.com/merge-sort
10
Mergesort: animation
50 reverse-sorted items
algorithm position
in order
current subarray
not in order
http://www.sorting-algorithms.com/merge-sort
11
Mergesort: empirical analysis
13
Divide-and-conquer recurrence: proof by picture
Pf 1. [assuming N is a power of 2]
D (N) N = N
D (N / 2) D (N / 2) 2 (N/2) = N
T(N) = N lg N
14
Divide-and-conquer recurrence: proof by induction
Pf 2. [assuming N is a power of 2]
Base case: N = 1.
Inductive hypothesis: D (N) = N lg N.
Goal: show that D (2N) = (2N) lg (2N).
= 2 N lg N + 2N inductive hypothesis
= 2 N lg (2N) QED
15
Mergesort: number of array accesses
Key point. Any algorithm with the following structure takes N log N time:
A C D G H I M N U V B E F J O P Q R S T
A B C D E F G H I J M N O P Q R S T U V
merged result
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);
}
18
Mergesort with cutoff to insertion sort: visualization
3.2 Mergesort
! 235
first subarray
second subarray
first merge
result
19
Mergesort: practical improvements
A B C D E F G H I J M N O P Q R S T U V
A B C D E F G H I J 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);
}
20
Mergesort: practical improvements
Eliminate the copy to the auxiliary array. Save time (but not space)
by switching the role of the input and auxiliary array in each recursive call.
private static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi)
{
int i = lo, j = mid+1;
for (int k = lo; k <= hi; k++)
{
if (i > mid) aux[k] = a[j++];
else if (j > hi) aux[k] = a[i++];
else if (less(a[j], a[i])) aux[k] = a[j++]; merge from a[] to aux[]
else aux[k] = a[i++];
}
}
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
{
if (hi <= lo) return;
int mid = lo + (hi - lo) / 2;
assumes aux[] is initialize to a[] once,
sort (aux, a, lo, mid);
sort (aux, a, mid+1, hi); before recursive calls
merge(a, aux, lo, mid, hi);
}
Arrays.sort(a)
http://www.java2s.com/Open-Source/Java/6.0-JDK-Modules/j2me/java/util/Arrays.java.html
22
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
Algorithms
comparators
Basic plan.
Pass through array, merging subarrays of size 1.
Repeat for subarrays of size 2, 4, 8, ....
a[i]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
sz = 1 M E R G E S O R T E X A M P L E
merge(a, aux, 0, 0, 1) E M R G E S O R T E X A M P L E
merge(a, aux, 2, 2, 3) E M G R E S O R T E X A M P L E
merge(a, aux, 4, 4, 5) E M G R E S O R T E X A M P L E
merge(a, aux, 6, 6, 7) E M G R E S O R T E X A M P L E
merge(a, aux, 8, 8, 9) E M G R E S O R E T X A M P L E
merge(a, aux, 10, 10, 11) E M G R E S O R E T A X M P L E
merge(a, aux, 12, 12, 13) E M G R E S O R E T A X M P L E
merge(a, aux, 14, 14, 15) E M G R E S O R E T A X M P E L
sz = 2
merge(a, aux, 0, 1, 3) E G M R E S O R E T A X M P E L
merge(a, aux, 4, 5, 7) E G M R E O R S E T A X M P E L
merge(a, aux, 8, 9, 11) E G M R E O R S A E T X M P E L
merge(a, aux, 12, 13, 15) E G M R E O R S A E T X E L M P
sz = 4
merge(a, aux, 0, 3, 7) E E G M O R R S A E T X E L M P
merge(a, aux, 8, 11, 15) E E G M O R R S A E E L M P T X
sz = 8
merge(a, aux, 0, 7, 15) A E E E E G L M M O P R R S T X
Trace of merge results for bottom-up mergesort
24
Bottom-up mergesort: Java implementation
input
1 5 10 16 3 4 23 9 13 2 7 8 12 14
first run
1 5 10 16 3 4 23 9 13 2 7 8 12 14
second run
1 5 10 16 3 4 23 9 13 2 7 8 12 14
1 3 4 5 10 16 23 9 13 2 7 8 12 14
Tradeoff. Fewer passes vs. extra compares per pass to identify runs.
27
Timsort
Natural mergesort.
Use binary insertion sort to make initial runs (if needed).
A few more clever optimizations.
Tim Peters
Intro
-----
This describes an adaptive, stable, natural mergesort, modestly called
timsort (hey, I earned it <wink>). It has supernatural performance on many
kinds of partially ordered arrays (less than lg(N!) comparisons needed, and
as few as N-1), yet as fast as Python's previous highly tuned samplesort
hybrid on random arrays.
In a nutshell, the main routine marches over the array once, left to right,
alternately identifying the next run, then merging it into the previous
runs "intelligently". Everything else is complication for speed, and some
hard-won measure of memory efficiency.
...
http://www.python.org/dev/peps/pep-0020/
http://westmarch.sjsoft.com/2012/11/zen-of-python-poster/
29
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
Algorithms
comparators
Example: sorting.
can access information
Model of computation: decision tree. only through compares
(e.g., Java Comparable framework)
Cost model: # compares.
Upper bound: ~ N lg N from mergesort.
Lower bound:
Optimal algorithm:
31
Decision tree (for 3 distinct keys a, b, and c)
a<b
height of tree =
yes no worst-case number
of compares
code between compares
(e.g., sequence of exchanges)
b<c a<c
yes no yes no
yes no yes no
Pf.
Assume array consists of N distinct values a through a .
1 N
33
Compare-based lower bound for sorting
Pf.
Assume array consists of N distinct values a through a .
1 N
2 h # leaves N !
h lg ( N ! ) ~ N lg N
Stirling's formula
34
Complexity of sorting
Example: sorting.
Model of computation: decision tree.
Cost model: # compares.
Upper bound: ~ N lg N from mergesort.
Lower bound: ~ N lg N.
Optimal algorithm = mergesort.
35
Complexity results in context
Lower bound may not hold if the algorithm can take advantage of:
37
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
Algorithms
comparators
39
Sort countries by total medals
40
Sort music library by artist
41
Sort music library by song name
42
Comparable interface: review
44
Comparator interface: system sort
Bottom line. Decouples the definition of the data type from the
definition of what it means to compare two objects of that type.
45
Comparator interface: using with our sorting libraries
46
Comparator interface: implementing
To implement a comparator:
Define a (nested) class that implements the Comparator interface.
Implement the compare() method.
public class Student
{
private final String name;
private final int section;
...
To implement a comparator:
Define a (nested) class that implements the Comparator interface.
Implement the compare() method.
48
2.2 M ERGESORT
mergesort
bottom-up mergesort
sorting complexity
Algorithms
comparators
A stable sort preserves the relative order of items with equal keys.
50
Stability
1 0 A1 B1 A2 A3 B2
2 1 A1 A2 B1 A3 B2
3 2 A1 A2 A3 B1 B2
4 4 A1 A2 A3 B1 B2
A1 A2 A3 B1 B2
1 A1 B2 B3 B4 B1
A1 B2 B3 B4 B1
Pf by counterexample. Long-distance exchanges.
54
Stability: mergesort
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);
}
0 1 2 3 4 5 6 7 8 9 10
A1 A2 A3 B D A4 A5 C E F G
selection N2 N2 N2 N exchanges
tight code;
shell N log3 N ? c N 3/2
subquadratic
N log N guarantee;
merge N lg N N lg N N lg N
stable
improves mergesort
timsort N N lg N N lg N
when preexisting order
57