#include <iostream>
void heapAdjust(int a[],int s,int e)
{
int tmp = a[s];
int j = 2*s + 1;
int c = s;
for(; j <= e;c = j,j = 2*j + 1) {
if( (j + 1) <= e && a[j] < a[j + 1]))
j++;
if(tmp > a[j]) break;
else {
a[c] = a[j];
a[j] = tmp;
}
}
}
void heapSort(int a[],int len)
{
for(int i = len/2 - 1; i >= 0; i--)
heapAdjust(a,i,len - 1);
for(int i = len - 1; i >= 0; i--) {
std::swap(a[i],a[0]);
heapAdjust(a,0,i - 1);
}
}
void printArray(int a[],int len)
{
for(int i = 0; i < len; i++)
std::cout << i << " ";
std::cout << std::endl;
}
int main(int argh,char** argv)
{
int a[] = { 1,4,2,5,7,9,6,10 };
int len = sizeof(a)/sizeof(a[0]);
printArray(a,len);
heapSort(a,len);
printArray(a,len);
}