\\
Sorts the heap (it is no longer a heap after the call)
Key Facts
Gyroscopic Couple: The rate of change of angular momentum () = (In the limit).- = Moment of Inertia.
- = Angular velocity
- = Angular velocity of precession.
Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.
Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.
Definition
The sort_heap() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.Interface
#include <algorithm> template < class RandomAccessIterator > void sort_heap( RandomAccessIterator first, RandomAccessIterator last ); template < class RandomAccessIterator, class Predicate > void sort_heap( RandomAccessIterator first, RandomAccessIterator last, Predicate comp );Parameters:
Parameter | Description |
---|---|
first | A random-access iterator addressing the position of the first element in the target heap |
last | A random-access iterator addressing the position one past the final element in the target heap |
comp | User-defined predicate function object that defines sense in which one element is less than another. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied |
Description
Sort_heap function converts a heap into a sorted range (ascending order). It does the same thing than doing a pop_heap on the heap until no more element is in the heap. The first version compares objects usingoperator<
and the second compares objects using a function object comp
.References
Example:
Example - sort_heap algorithm
Problem
This program illustrates the use of the STL sort_heap() algorithm (default
version) to sort a (maximum) heap of integers into ascending order.
Workings
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a[] = {100, 19, 36, 17, 3, 25, 1, 2, 7}; vector<int> v(a, a+9); cout <<"\nHere are the values in the heap:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; cout <<"\nNow we sort these values into ascending order."; sort_heap(v.begin(), v.end()); cout <<"\nHere is the result:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" ";
Solution
Output:
Here are the values in the heap:
100 19 36 17 3 25 1 2 7 Now we sort these values into ascending order. Here is the result:
1 2 3 7 17 19 25 36 100
100 19 36 17 3 25 1 2 7 Now we sort these values into ascending order. Here is the result:
1 2 3 7 17 19 25 36 100
References