restructure and add ipi texs
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
template <class C>
|
||||
inline void reheap (C& a, int n, int i) {
|
||||
while (2*i+1<n)
|
||||
{
|
||||
int l = 2*i+1;
|
||||
int r = l+1;
|
||||
int k = ((r<n) && (a[r]>a[l])) ? r : l;
|
||||
if (a[k]<=a[i]) break;
|
||||
std::swap(a[k], a[i]);
|
||||
i = k;
|
||||
}
|
||||
}
|
||||
|
||||
template <class C>
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user