参考写得一个用递归实现的归并排序 (mergeSort)程序(JAVA)
public class Merge_Sort {
private Merge_Sort(){}
public static void merge(int[] a){
int[] aux = new int[a.length];
if(a.length>2)
merge(a,aux,0,(a.length-1)/2,a.length-1);
sort(a,aux,0,(a.length-1)/2,a.length-1);
}
public static void merge(int[] a,int[] aux,int lo,int mid,int hi){
int midd = (mid-lo)/2;
if(midd!=0)
merge(a,aux,lo,lo+midd,mid);
sort(a,aux,lo,lo+midd,mid);
midd = (hi-mid-1)/2;
if(midd!=0)
merge(a,aux,mid+1,hi-midd-1,hi);
sort(a,aux,mid+1,hi-midd-1,hi);
}
public static void sort(int[] a,int[] aux,int lo,int mid,int hi){
int i=lo,j=mid+1,k=lo;
while(i<=mid && j<=hi)
if(a[i]<a[j])
aux[k++]=a[i++];
else aux[k++]=a[j++];
while(i<=mid)
aux[k++]=a[i++];
while(j<=hi)
aux[k++]=a[j++];
for(int tt=lo;tt<=hi;tt++)
a[tt]=aux[tt];
System.out.println("lo="+lo+" hi="+hi);
System.out.print("aux=");
Array.print(aux);
//System.out.print("a =");
//Array.print(a);
}
public static void main(String[] args){
Array a = new Array(19);
//int[]b={62,20};
//Array.print(b);
merge(a.array);
Array.print(a.array);
}
}