template inline void reheap (C& a, int n, int i) { while (2*i+1a[l])) ? r : l; if (a[k]<=a[i]) break; std::swap(a[k], a[i]); i = k; } } template void heapsort (C& a) { // build the heap by reheaping from the rear for (int i=a.size()-1; i>=0; i--) reheap(a, a.size(), i); // build the sorted list by popping the heap for (int i=a.size()-1; i>=0; i--) { std::swap(a[0],a[i]); reheap(a, i, 0); } }