归并排序的实现_by watson.long
归并排序的实现_by watson.long
————————————————————————————
#include "stdio.h"
void mergesort(int a[],int r[],int left,int right);
void merge(int a[],int b[],int left,int mid,int right);
void main(int argc,char *argv[])
{
int data[10]={9,1,2,4,6,10,23,5,3,56};
int result[10]={0};
int i=0;
/*merge sort*/
mergesort(data,result,0,9);
return;
}
/*merge sort*/
void mergesort(int a[],int r[],int left,int right)
{
int mid;
if(left<right)
{
mid=(left+right)/2;
mergesort(a,r,left,mid);
mergesort(a,r,mid+1,right);
merge(a,r,left,mid,right);
}
}
void merge(int a[],int b[],int left,int mid,int right)
{
int k;
int s1,s2;
int i=left;
for(k=left;k<=right;k++) b[k]=a[k];
s1=left;
s2=mid+1;
while(s1<=mid&&s2<=right)
{
if(b[s1]<b[s2]) a[i++]=b[s1++];
else a[i++]=b[s2++];
}
while(s1<=mid) a[i++]=b[s1++];
while(s2<=right) a[i++]=b[s2++];
return;
}