#define LeftChild(i)(2 * (i) + 1) void PercDown(int a[], int i, int n) { int c, t; for(t = a[i]; LeftChild(i) < n; i = c) { c = LeftChild(i); if(c != n - 1 && a[c + 1] > a[c]) c++; if(t < a[c]) a[i] = a[c]; else break; } a[i] = t; } void Heapsort(int a[], int n) { int i, t; i = n / 2 + 1; while(i--) PercDown(a, i, n); i = n; while(--i) { t = a[0]; a[0] = a[i]; a[i] = t; PercDown(a, 0, i); } }