/**
* Created by lxw, liwei4939@126.com on 2017/9/24.
*/
public class Merge {
private static Comparable[] aux;
public static void sort(Comparable[] a){
aux=new Comparable[a.length];
sort(a, 0, a.length-1);
}
private static void sort(Comparable[] a, int lo, int hi){
if(hi<=lo)
return;
int mid= lo +(hi-lo)/2;
sort(a,lo, mid);
sort(a,mid+1, hi);
merge(a,lo,mid,hi);
}
public static void merge(Comparable[] a, int lo, int mid, int hi){
int i=lo, j=mid+1;
for(int k=0;k<=hi; k++)
aux[k] = a[k];
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++];
}
}
}
算法 第四版 归并排序
最新推荐文章于 2021-12-17 20:00:00 发布