#include <iostream>
#include <stdio.h>
void disp(int a[], int n);
void MergeSort(int a[], int n);
void MergePass(int a[], int length, int n);
void merge(int a[], int low, int mid, int high);
int main(){
int n=10;
int a[] ={2,5,1,7,10,6,9,4,3,8};
printf("排序前:\n");
disp(a,n);
MergeSort(a,n);
printf("排序后:\n");
disp(a,n);
}
void MergeSort(int a[], int n) {
int length;
for(length=1;length<n;length=2*length)
MergePass(a,length,n);
}
void MergePass(int a[], int length, int n) {
int i=0;
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 merge(int a[], int low, int mid, int high) {
int *tmpa;
int i=low,j=mid+1,k=0;
tmpa=(int *) malloc((high-low+1) * sizeof(int));
while(i<=mid&&a