# 算法|归并排序

1. 先将左半边数组进行排序,
2. 再将右半边元素进行排序,
3. 最后将左边和右边的有序数组进行合并结果

merge(int[] a, int low, int mid, int height)

private static void merge(int[] a, int low, int mid, int height) {
int i = low;
int j = mid + 1;

//copy the a to auxiliary
for (int k = low; k <= height; k++) {
auxiliary[k] = a[k];
}

//merge left and right
for (int k = low; k <= height; k++) {
if (i > mid) {
a[k] = auxiliary[j++];
} else if (j > height) {
a[k] = auxiliary[i++];
} else if (a[i] >= a[j]) {
a[k] = auxiliary[j++];
} else {
a[k] = auxiliary[i++];
}
}
}

    static int[] nums = new int[]{
1, 15, 3, 78,
34, 23, 46, 2,
8, 34, 57, 50,
200, 123, 66, 43,
33, 100, 356, 21};

static int[] auxiliary = new int[nums.length];

public static void main(String[] args) {

System.out.println("before Merge sort " + Arrays.toString(nums));
//        topToBottomSort(nums, 0, nums.length - 1);
bottomToTopSort(nums);

System.out.println("after Merge sort " + Arrays.toString(nums));
}

public static void topToBottomSort(int[] nums, int low, int height) {
if (low >= height) return;

int mid = low + (height - low) / 2;

topToBottomSort(nums, low, mid);// sort left
topToBottomSort(nums, mid + 1, height);//sort right

merge(nums, low, mid, height); // merge left and right
}

a[0]和a[1]归并后,紧接着就是a[2]和a[3]合并,然后是a[4]...a[7]的排序,同理也是一样的排序流程,将他们递归的分割到只有一个元素的数组时,就可以开始归并排序,和上面的流程是一样的,依次类推,最后归并到a[0]...a[15]

    public static void bottomToTopSort(int[] nums) {
final int N = nums.length;
for (int size = 1; size < N; size = size + size) {//the size is length of sub array
for (int low = 0; low < N - size; low += size + size) {
merge(nums, low, low + size - 1, Math.min(low + size + size - 1, N - 1));
}
}
}

private static void merge(int[] a, int low, int mid, int height) {
int i = low;
int j = mid + 1;

//copy the a to auxiliary
for (int k = low; k <= height; k++) {
auxiliary[k] = a[k];
}

//merge left and right
for (int k = low; k <= height; k++) {
if (i > mid) {
a[k] = auxiliary[j++];
} else if (j > height) {
a[k] = auxiliary[i++];
} else if (a[i] >= a[j]) {
a[k] = auxiliary[j++];
} else {
a[k] = auxiliary[i++];
}
}
}