#include<iostream>
#include<cstdio>
#include<malloc.h>
using namespace std;
void disp(int a[],int n){
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
printf("\n");
}
void merge(int a[],int low,int mid,int high){
int *temp;
int i=low,j=mid+1,k=0;
temp = (int*)malloc(sizeof(int)*(high-low+1));
while(i<=mid&&j<=high){
if(a[i]<=a[j]){
temp[k]=a[i];
i++;k++;
}
else{
temp[k]=a[j];
j++;k++;
}
}
while(i<=mid){
temp[k]=a[i];
i++;k++;
}
while(j<=high){
temp[k]=a[j];
j++;k++;
}
for(k=0,i=low;i<=high;k++,i++)
a[i]=temp[k];
free(temp);
}
void mergePass(int a[],int length,int n){
int i;
for(i=0;i+2*length-1<n;i=i+2*length){
merge(a,i,i+length-1,i+2*length-1);
}
if(i+length-1<n)
merge(a,i,i+length-1,n-1);
}
void MergeSort(int a[],int n){
int length;
for(length=1;length<n;length=2*length){
mergePass(a,length,n);
}
}
int main(){
int n=10;
int a[]={51,12,23,68,92,45,76,80,30,63};
printf("排序前的数组为:");
disp(a,n);
MergeSort(a,n);
printf("排序后的数组为:");
disp(a,n);
}