归并排序
二路归并算法:
merge(R,A,s,m,t)
/*R->R[m],R[m+1]->R[t]=>A->A[t]*/
int s,m,t;
{int i,j,k;
i=s;
j=m+1;
k=s;
while((i<=m)&&(j<=t))
if(R.key<=R[j].key)
{A[k]=R;
i++;
k++;
}
else
{A[k]=R[j];
j++;
k++;
}
while(i<=m)
{A[k]=R;
i++;
k++;
}
while(j<=t)
{A[k]=R[j];
j++;
k++;
}
}
对数组R[n]做一趟归并,R[n]以长度c分段:
mergepass(R,A,n,c)
recdtype R[],A[];
int n,c;
{int i,j;
i=1;
while(i+2*c-1<=n)
{merge(R,A,i,i+c-1,i+2*c-1);
i=i+2*c;
}
if(i+c-1 |