[求助]归并排序求助
求归并排序算法的比较和交换次数,达人赐教~~~
#include<stdio.h>
void csort(int,int); //归并排序
void comf(int,int,int); //将数整理成有序数列
#define N 10000
int a[N],b[N];
int main()
{
int i,n;
scanf("%d",&n);
if(n<N)
{
for (i=0;i<n;i++)
scanf("%d",&a[i]);
csort(0,n-1);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
else
return 0;
}//main
void csort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
csort(low,mid);
csort(mid+1,high);
comf(low,mid,high);
}
}//csort
void comf(int low,int mid,int high)
{
int p=low,q=mid+1;
int r=low;
while(p<=mid&&q<=high)
{
if(a[p]<a[q])
b[r++]=a[p++];
else b[r++]=a[q++];
}
if(p<=mid)
{
while(r<=high)
b[r++]=a[p++];
}
else
{
while(r<=high)
b[r++]=a[q++];
}
for(r=low;r<=high;r++)
a[r]=b[r];
}//comf
vc++通过了……