hoanhap
void Merge(int a[], int d1, int c1, int c2, int b[])
{
int i,j,k;
i=k=d1;
j=c1+1;
while ((i<=c1) && (j<=c2))
if (a[i]<a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
while (i<=c1)
b[k++]=a[i++];
while (j<=c2)
b[k++]=a[j++];
}
void mpass(int a[], int b[], int n, int l)
{
int i,j;
i=-1;
while (i+2*l<n)
{
Merge(a,i+1,i+l,i+2*l,b);
i+=2*l;
}
if (i+l<n-1)
Merge (a,i+1,i+l,n-1,b);
else
for (j=i+1;j<n;j++)
b[j]=a[j];
}
void mergesort(int a[], int n)
{
int l, b[100];
l=1;
while (l<n)
{
mpass(a,b,n,l);
l=2*l;
mpass(b,a,n,l);
l=2*l;
}
}
Bạn đang đọc truyện trên: AzTruyen.Top