void heapAdjust(int a[],int s,int n)
{
int largest=s;
if(2*s<n&&a[2*s]>a[s])largest=2*s;
if(2*s+1<n&&a[2*s+1]>a[largest])largest=2*s+1;
if(largest==s) return ;
else {
swap(a[s],a[largest]);
heapAdjust(a,largest,n);
}
}
void heapSort(int a[],int n)
{
int k=n;
int i=n/2;
while(i>=0)
{
heapAdjust(a,i,n);
i--;
}
while(n>=1)
{
swap(a[0],a[n-1]);
n--;
heapAdjust(a,0,n);
}
for(int j=0;j<k;j++)
{
cout<<a[j]<<" ";
}
}
int main()
{
int a[]={1,3,2,6,7,4,5,9};
heapSort(a,8);
system("pause");
return 0;
}
{
int largest=s;
if(2*s<n&&a[2*s]>a[s])largest=2*s;
if(2*s+1<n&&a[2*s+1]>a[largest])largest=2*s+1;
if(largest==s) return ;
else {
swap(a[s],a[largest]);
heapAdjust(a,largest,n);
}
}
void heapSort(int a[],int n)
{
int k=n;
int i=n/2;
while(i>=0)
{
heapAdjust(a,i,n);
i--;
}
while(n>=1)
{
swap(a[0],a[n-1]);
n--;
heapAdjust(a,0,n);
}
for(int j=0;j<k;j++)
{
cout<<a[j]<<" ";
}
}
int main()
{
int a[]={1,3,2,6,7,4,5,9};
heapSort(a,8);
system("pause");
return 0;
}