归并排序
#include<stdio.h>
void Sort_Message(int *a,int *teamp,int start,int end);
void Merge_Sort(int *a,int *teamp,int start,int mid,int end);
int main()
{
int a[9]={3,23,54,1,564,23,1,0,65};
int teamp[9];
int i;
Sort_Message(a,teamp,0,8);
for(i=0;i<9;i++)
printf("%d ",a[i]);
return 0;
}
void Sort_Message(int *a,int *teamp,int start,int end)
{
if(start<end)
{
int mid=(start+end)/2;
Sort_Message(a,teamp,start,mid);
Sort_Message(a,teamp,mid+1,end);
Merge_Sort(a,teamp,start,mid,end);
}
}
void Merge_Sort(int *a,int *teamp,int start,int mid,int end)
{
int left_start=start;
int left_end=mid;
int right_start=mid+1;
int right_end=end;
int idx=start;
while((left_start<=left_end)&&(right_start<=right_end))
{
if(a[left_start]>a[right_start])
{
teamp[idx++]=a[right_start++];
}
else
{
teamp[idx++]=a[left_start++];
}
}
while(left_start<=left_end)
{
teamp[idx++]=a[left_start++];
}
while(right_start<=right_end)
{
teamp[idx++]=a[right_start++];
}
for(idx=start;idx<=end;idx++)
{
a[idx]=teamp[idx];
}
}