#include <stdio.h>
#include <stdlib.h>
//两个有序数组合并
void MergeArray(int a[], int front, int mid, int last, int b[])
{
int i=front;
int j=mid+1;
int k=0;
while(i<=mid && j<=last)
{
if(a[i]<a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
while(i<=mid)
b[k++]=a[i++];
while(j<=last)
b[k++]=a[j++];
for(i=0;i<k;i++)
a[front+i]=b[i];
}
//左右有序 然后合并
void MergeSort(int a[],int front, int last,int b[])
{
int mid=0;
if(front<last)
{
mid=(front+last)/2;
MergeSort(a,front,mid,b);
MergeSort(a,mid+1,last,b);
MergeArray(a,front,mid,last,b);
}
}
int main(int argc, char *argv[])
{
int i;
int a[]={4,5,2,6,9,7,5,74,56,89,132};
int b[]={4,5,2,6,9,7,5,74,56,89,132};
MergeSort(a,0,10,b);
for(i=0;i<11;i++)
printf("%d ",a[i]);
printf("\n");
system("PAUSE");
return 0;
}
12-22