代码
private static void MergeSort(int[] a) {
System.out.println("开始排序");
Sort(a, 0, a.length - 1);
}
private static void Sort(int[] a, int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
Sort(a, left, mid);
Sort(a, mid + 1, right);
merge(a, left, mid, right);
common.print(a);
}
private static void merge(int[] r, int i, int m, int n) {
int[] rf = new int[r.length];
int k = i;
int tmpI = i;
int j = m + 1;
for (; i <= m && j <= n; k++) {
if (r[i] < r[j]) {
rf[k] = r[i++];
} else {
rf[k] = r[j++];
}
}
while (i <= m) {
rf[k++] = r[i++];
}
while (j <= n) {
rf[k++] = r[j++];
}
i = tmpI;
while (i <= n) {
r[i] = rf[i];
i++;
}
}