#include<iostream>
using namespace std;
void HeapAdjust(int H[] ,int s,int m)
{
int rc=H[s];
int j;
for(j=s*2;j<=m;j*=2)
{
if(j<m && H[j]<H[j+1])
j++;
if(rc>H[j])break;
H[s]=H[j];
s=j;
}
H[s]=rc;
}
void HeapSort(int H[],int length)
{
for(int i=length/2;i>0;--i)
{
HeapAdjust(H,i,length);
}
for(i=length;i>1;--i)
{
int temp;
temp=H[1];H[1]=H[i];H[i]=temp;
HeapAdjust(H,1,i-1);
}
}
int main()
{
int H[11]={0,9,8,5,6,4,7,1,3,2,10};
HeapSort(H,10);
for(int i=1;i<=10;i++)
cout<<H[i]<<" ";
cout<<endl;
return 0;
}
|