原文:https://blog.csdn.net/h3243212/article/details/51345445#%E5%86%99%E5%9C%A8%E5%89%8D%E9%9D%A2
标签: algorithms
本文介绍几种常见排序算法(选择排序,插入排序,希尔排序,归并排序,快速排序,堆排序),对算法的思路、性质、特点、具体步骤、java实现以及trace图解进行了全面的说明。最后对几种排序算法进行了比较和总结。
git clone [email protected]:brianway/algorithms-learning.git
java:
Interface Comparable
Java中很多类已经实现了Comparable
接口,用户也可自定义类型实现该接口
total order:
注意: The <=
operator for double is not a total order ,violates totality: (Double.NaN <=
Double.NaN) is false
通用代码:
// Less. Is item v less than w ?
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
//Exchange. Swap item in array a[] at index i with the one at index j
private static void exch(Comparable[] a,, int i, int j) {
Comparable swap = a[i];
a[i] = a[j];
a[j] = swap;
}
思路:
- 在第i次迭代中,在剩下的(即未排序的)元素中找到最小的元素
- 将第i个元素与最小的元素交换位置
现象:
步骤:
java实现:
public static void sort(Comparable[] a) {
int N = a.length;
for (int i = 0; i < N; i++) {
int min = i;
for (int j = i+1; j < N; j++) {
if (less(a[j], a[min])) min = j;
}
exch(a, i, min);
}
}
特点:
思路:
- 在第i次迭代中,将第i个元素与每一个它左边且比它大的的元素交换位置
现象:
步骤:
a[i]
with each larger entry to its left.java实现:
public static void sort(Comparable[] a) {
int N = a.length;
for (int i = 0; i < N; i++) {
for (int j = i; j > 0 && less(a[j], a[j-1]); j--) {
exch(a, j, j-1);
}
}
}
inversion(倒置):An inversion is a pair of keys that are out of order
部分有序:An array is partially sorted if the number of inversions is ≤ c N.
特点:
希尔排序是基于插入排序的。
思路:
- Move entries more than one position at a time by h-sorting the array
- 按照h的步长进行插入排序
现象:
性质:
- 递增数列一般采用3x+1:1,4,13,40,121,364…..,使用这种递增数列的希尔排序所需的比较次数不会超过N的若干倍乘以递增数列的长度。
- 最坏情况下,使用3x+1递增数列的希尔排序的比较次数是O(N^(3/2))
java实现:
public static void sort(Comparable[] a) {
int N = a.length;
// 3x+1 increment sequence: 1, 4, 13, 40, 121, 364, 1093, ...
int h = 1;
while (h < N/3) h = 3*h + 1;
while (h >= 1) {
// h-sort the array
for (int i = h; i < N; i++) {
for (int j = i; j >= h && less(a[j], a[j-h]); j -= h) {
exch(a, j, j-h);
}
}
h /= 3;
}
}
目标:Rearrange array so that result is a uniformly random permutation
shuffle sort思路
- 为数组的每一个位置生成一个随机实数
- 排序这个生成的数组
Knuth shuffle demo
- In iteration i, pick integer r between 0 and i uniformly at random.
- Swap
a[i]
anda[r]
.
correct variant: between i and N – 1
下面看看这两种排序算法
思路:
- Divide array into two halves.
- Recursively sort each half.
- Merge two halves.
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]
步骤:
aux[]
中,再归并回a[]
中。merging java实现:
// stably merge a[lo .. mid] with a[mid+1 ..hi] using aux[lo .. hi]
private static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) {
// precondition: a[lo .. mid] and a[mid+1 .. hi] are sorted subarrays
// copy to aux[]
for (int k = lo; k <= hi; k++) {
aux[k] = a[k];
}
// merge back to a[]
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++];
}
}
mergesort java实现:
// mergesort a[lo..hi] using auxiliary array aux[lo..hi]
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); //归并结果
}
自顶向下的归并排序的轨迹图
由图可知,原地归并排序的大致趋势是,先局部排序,再扩大规模;先左边排序,再右边排序;每次都是左边一半局部排完且merge了,右边一半才开始从最局部的地方开始排序。
改进
思路:
- 先归并微型数组,从两两归并开始(每个元素理解为大小为1的数组)
- 重复上述步骤,逐步扩大归并的规模,2,4,8…..
java实现:
public class MergeBU{
private static void merge(...){ /* as before */ }
public static void sort(Comparable[] a){
int N = a.length;
Comparable[] aux = new Comparable[N];
for (int sz = 1; sz < N; sz = sz+sz)
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));
}
}
自底向上的归并排序的轨迹图
由图可知,自底向上归并排序的大致趋势是,先局部排序,逐步扩大到全局排序;步调均匀,稳步扩大
思路:
- Shuffle the array.
- Partition(切分) so that, for some j
- entry a[j] is in place
- no larger entry to the left of j
- no smaller entry to the right of j
- Sort each piece recursively.
其中很重要的一步就是Partition(切分),这个过程使得满足以下三个条件:
partition java实现
// partition the subarray a[lo..hi] so that a[lo..j-1] <= a[j] <= a[j+1..hi]
// and return the index j.
private static int partition(Comparable[] a, int lo, int hi) {
int i = lo;
int j = hi + 1;
Comparable v = a[lo];
while (true) {
// find item on lo to swap
while (less(a[++i], v))
if (i == hi) break;
// find item on hi to swap
while (less(v, a[--j]))
if (j == lo) break; // redundant since a[lo] acts as sentinel
// check if pointers cross
if (i >= j) break;
exch(a, i, j);
}
// put partitioning item v at a[j]
exch(a, lo, j);
// now, a[lo .. j-1] <= a[j] <= a[j+1 .. hi]
return j;
}
快排java实现:
public static void sort(Comparable[] a) {
StdRandom.shuffle(a);
sort(a, 0, a.length - 1);
}
// quicksort the subarray from a[lo] to a[hi]
private static void sort(Comparable[] a, int lo, int hi) {
if (hi <= lo) return;
int j = partition(a, lo, hi);
sort(a, lo, j-1);
sort(a, j+1, hi);
assert isSorted(a, lo, hi);
}
快排的轨迹图
由图可知,和归并排序不同,快排的大致趋势是,先全局大体有个走势——左边比右边小,逐步细化到局部;也是先左后右;局部完成时全部排序也就完成了。
一些实现的细节:
性质:
改进
思路:
- Let v be partitioning item a[lo].
- Scan i from left to right.
- (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
- (a[i] > v): exchange a[gt] with a[i]; decrement gt
- (a[i] == v): increment i
主要是通过增加一个指针来实现的。普通的快拍只有lo和high两个指针,故只能记录大于
(high右边)和小于
(lo左边)两个区间,等于
只能并入其中一个;这里增加了使用了lt,i,gt三个指针,从而达到记录大于
(gt右边)、小于
(lt左边)和等于
(lt和i之间)三个区间。
三切分的示意图
三向切分的java实现:
// quicksort the subarray a[lo .. hi] using 3-way partitioning
private static void sort(Comparable[] a, int lo, int hi) {
if (hi <= lo) return;
int lt = lo, gt = hi;
Comparable v = a[lo];
int i = lo;
while (i <= gt) {
int cmp = a[i].compareTo(v);
if (cmp < 0) exch(a, lt++, i++);
else if (cmp > 0) exch(a, i, gt--);
else i++;
}
// a[lo..lt-1] < v = a[lt..gt] < a[gt+1..hi].
sort(a, lo, lt-1);
sort(a, gt+1, hi);
}
思路:
- Create max-heap with all N keys.
- Repeatedly remove the maximum key.
堆排序主要分为两个阶段:
java实现如下:
public static void sort(Comparable[] pq) {
int N = pq.length;
//堆的构造
for (int k = N/2; k >= 1; k--)
sink(pq, k, N);
//下沉排序
while (N > 1) {
exch(pq, 1, N--);
sink(pq, 1, N);
}
}
堆排序的轨迹图
由图看出,堆排序的趋势是,堆构造阶段,大致是降序的走势,到了下沉阶段,从右到左(或者说从后往前)逐步有序
Significance: In-place sorting algorithm with N log N worst-case.
缺点
排序算法总结表
最好情况和最坏情况:参见上面的表格
关于稳定性:
关于额外空间:除了归并排序需要线性的额外空间,其他都是in-place的
aux[]
needs to be of size N for the last merge.)