#include <stdio.h>
void temp(int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
}
void maxheap(int *a,int i,int end)
{
//if(2*i+1>end) return;
int son,dad;
dad=i;
if(2*i+2<=end)
{
son=a[2*i+1]>a[2*i+2]?2*i+1:2*i+2;
}
else son=2*i+1;
while(a[son]>a[dad])
{
temp(a[son],a[dad]);
dad=son;
if(2*dad+2<=end)
{
son=a[2*dad+1]>a[2*dad+2]?2*dad+1:2*dad+2;
}
else son=2*dad+1;
}
}
void heapsort(int *a,int start,int end)
{
for(int i=(end+1)/2-1;i>=start;i--)
{
maxheap(a,i,end);
}
while(end>0)
{
temp(a[0],a[end]);
end--;
maxheap(a,0,end);
}
}
int main()
{
int a[10]={3,87,2,93,78,56,61,38,12,40};
//quicksort(a,0,19);
//mergesore(a,0,19);
heapsort(a,0,9);
}
```