归并排序与排序下界
Merge Sort
Basic idea of mergesort is divide-and-conquer paradigm.
- Divide array into two halves.
- Recursively sort each half.
- Merge two sorted halves using two pointers.
In empirical analysis of merge sort, # compares is denoted by
Proposition. if
We prove this by induction with base case
Mergesort uses extra space proportional to
Definition. A sorting algorithm is in-place if it uses
Examples. Insertion sort, selection sort, shellsort.
Code Implementation
public class Merge {
private static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) {
assert isSorted(a, lo, mid);
assert isSorted(a, mid + 1, 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++];
}
assert isSorted(a, lo, hi);
}
// Recursive version
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) {
aux = new Comparable[a.length];
sort(a, aux, 0, a.length - 1);
}
// Non-recursive bottom-up version
public static void sort(Comparable[] a) {
int N = a.length;
Comparable[] aux = new Comparable[N];
for (int sz = 1; sz < N; sz <<= 1)
for (int lo = 0; lo < N - sz; lo += sz+sz)
merge(a, aux, lo, lo + sz - 1, Math.min(lo + sz + sz - 1, N - 1));
}
}
Practical Improvements
-
Use insertion sort for small subarrays.
Mergesort has too much overhead for tiny subarrays. Cutoff to insertion sort for
items. javaprivate 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); } -
STOP if already sorted. (especially helpful for partially-ordered arrays.)
javaprivate 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); } -
Eliminate the copy to the auxiliary array.
Save time (not space) by switching the role of input and auxiliary array in each recursive call.
Complexity of Sorting
Computational complexity is a framework to study efficiency of algorithms for solving a particular problem
- Model of computation. Allowable operations.
- Cost model. Operation count(s).
- Upper bound. Cost guarantee provided by some algorithms for
. - Lower bound. Proven limit on cost guarantee by all algorithms for
. - Optimal algorithm. Algorithm with best possible cost guarantee for
.
We model the complexity of sorting problem as follows:
- Model of computation. Decision trees.
- Cost model. # compares.
- Upper bound.
from mergesort. - Lower bound. ?
- Optimal algorithm. ?
Decision Tree
Decision trees for compare-base sorting algorithms can access information only through compare.
Note that the height of the tree
Lower Bound for Sorting
Proposition. Any compare-base sorting algorithm must use at least
We prove this by establishing an inequality based on the number of leaves of decision tree.
- Binary tree of height
has at most leaves. # leaves . different orderings at least leaves. # leaves . - Worst case dictated by height
of decision tree.
The lower bound of sorting problem is thus determined by
We could see that the upper bound and the lower bound overlap each other
Note that quicksort (stay tuned) is NOT optimal: it needs

