void push(int i, int a[], int n)
{
int j, tg;
while (i<=n/2-1)
{
j=2*i+1;
if ((j<n-1) && (a[j]<a[j+1]))
j++;
if (a[j]>a[i])
{
tg=a[i];
a[i]=a[j];
a[j]=tg;
i=j;
}
else i=n;
}
}
void push2(int i, int a[], int n)
{
int j, tg=a[i];
while (i<=n/2-1)
{
j=2*i+1;
if ((j<n-1) && (a[j]<a[j+1]))
j++;
if (a[j]>a[i])
{
a[i]=a[j];
i=j;
}
else n=i;
}
a[i]=tg;
}
void heap(int a[], int n)
{
int i, tg;
for (i=n/2-1;i>=0;i--)
push(i,a,n);
for (i=n-1;i>0;i--)
{
tg=a[0];
a[0]=a[i];
a[i]=tg;
push(0,a,i);
}
}
void heap2(int a[], int n)
{
int i, tg;
for (i=n/2-1;i>=0;i--)
push(i,a,n);
for (i=n-1;i>0;i--)
{
tg=a[0];
a[0]=a[i];
a[i]=tg;
push(0,a,i);
}
}
Bạn đang đọc truyện trên: Truyen2U.Pro