//归并排序
class Merge{
static int []aux;
static void sort(int []a) {
aux = new int[a.length];
mergeSort(a, 0, a.length-1);
System.out.println(Arrays.toString(a));
}
static void mergeSort(int a[],int lo,int hi) {
// TODO Auto-generated method stub
if(lo<hi) {
int mid =lo+ (hi-lo)/2;
mergeSort(a, lo, mid);
mergeSort(a, mid+1, hi);
merge(a, mid, lo, hi);
}else {
return;
}
}
//合并有序数组
public static void merge(int a[],int mid,int low ,int hi) {
int i = low;
int j = mid +1;
for (int f=low;f<=hi;f++) {
aux[f] = a[f];
}
for(int k = low;k<=hi;k++) {
if(i>mid) {
a[k] = aux[j++];
}else if (j>hi) {
a[k] = aux[i++];
}else {
a[k] = aux[i]<aux[j]?aux[i++]:aux[j++];
}
}
System.out.println(Arrays.toString(a)+1);
}
}
归并排序
最新推荐文章于 2023-09-03 20:36:33 发布