import java.util.Scanner;
public class CMerge {
private int[] a;
private int size;
private int[] b;//辅助临时数组
public static void main(String[] args) {
CMerge merge = new CMerge();
merge.input();
merge.display();
merge.MergeSort(0, merge.getSize()-1);
merge.display();
}
public void MergeSort(int low, int high) {
if (low == high) {
return;
}
int mid = (low + high) / 2;
MergeSort(low, mid);
MergeSort(mid + 1, high);
merge(low, mid, high);
}
private void merge(int low, int mid, int high) {
int i, j, k;
i = low;
j = mid + 1;
k = 0;
while (i <= mid && j <= high) {
b[k++] = a[i] < a[j] ? a[i++] : a[j++];
}
while (i <= mid) {
b[k++] = a[i++];
}
while (j <= high) {
b[k++] = a[j++];
}
/*将low-high这个区间内的数字排序完后,重新赋值回原数组对应区间位置,等待下一次的归并排序*/
for (i = low, k = 0; i <= high; i++) {
a[i] = b[k++];
}
}
public void input() {
System.out.println("数组大小:");
Scanner scan = new Scanner(System.in);
size = scan.nextInt();
a = new int[size];
b = new int[size];
System.out.println("数组元素");
for (int i = 0; i < a.length; i++) {
a[i] = scan.nextInt();
}
System.out.println("输入完成");
}
public void display() {
for (int i = 0; i < b.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
public int getSize() {
return size;
}
}