1.算法解释
2.算法轨迹
3.算法的java实现
public static void merge(int a[], int low, int mid, int high) {
int i = low, j = mid + 1;
int[] aux = new int[a.length];//复制数组
for (int k = low; k < a.length; k++) { // 将a[low-high]复制到aux[low-high]中
aux[k] = a[k];
}
for (int k = low; k <= high; k++) {
if (i > mid)//左半边用尽,取右半边
a[k] = aux[j++];
else if (j > high)//右半边用户,取左半边元素
a[k] = aux[i++];
else if (aux[j] < aux[i])//右半边的当前元素小于左半边的当前元素(取右半边的元素)
a[k] = aux[j++];
else //右半边的当前元素大于等于左半边的当前元素(取左半边的元素)
a[k] = aux[i++];
}
}
//算法轨迹见算法四第172页
public static void sort(int a[], int low, int high) {
int mid = (high + low) / 2;
if (low < high) {
sort(a, low, mid);//将左半边排序
sort(a, mid + 1, high);//将右半边排序
merge(a, low, mid, high);//归并结果
}
}