Fork Copy int compare(int a, int b){ return a < b; } template void Pushdown(T *a, int i, int n, int (*compare)(T,T)){ int j = i, kt = 0; int max; while(j <= n / 2 && kt == 0){ if (2 * j == n){ max = 2 * j; } else if (compare(a[2 * j], a[2 * j + 1])) max= 2 * j + 1; else max = 2 * j; if (compare(a[j], a[max])){ Swap(a[j], a[max]); j = max; } else kt = 1; } } template void HeapSort(T *a, int n, int (*compare)(T, T)){ for (int i = (n - 1) / 2; i >= 0; i--) Pushdown(a, i, n - 1, compare); for (int i = n-1; i >= 1; i--){ Swap(a[0], a[i]); Pushdown(a, 0, i-1, compare); } }