归并排序”

递归下去 再合并上来

public static void mergeSort(int[] a, int left, int right) {
    if (left >= right) {
        return;
    }
    int center = (left + right) >> 1;
    // 等价于 (left+ right)/2
    mergeSort(a, left, center);
    mergeSort(a, center + 1, right);
    merger(a, left, center, right);
}

public static void merger(int[] a, int left, int center, int right) {
    int[] temArr = new int[right + 1];
    int mid = center + 1;// 有部分的遍历索引
    int index = left;// index 临时数组的索引,将排好序先放在这里 ,排序完成重新付给a
    int tmp = left; // 最后赋给原来的数组是遍历用

    // 从俩个数组中去除最小的数 放入临时数组 temArr中
    while (left <= center && mid <= right) {
        temArr[index++] = a[left] < a[mid] ? a[left++] : a[mid++];
    }
    // 剩余部分放入临时数组
    while (left <= center) {
        temArr[index++] = a[left++];
    }
    while (mid <= right) {
        temArr[index++] = a[mid++];
    }
    // 将临时数组 复制给原来的数组
    for (int i = tmp; i <= right; i++) {
        a[i] = temArr[i];
    }
    printSort(a);
}