C++ examples for STL Algorithm:sort_heap
Do Heap sort with algorithm make_heap and sort_heap
#include <iostream> #include <algorithm> #include <vector> #include <iterator> using namespace std; int main() //ww w . j a v a2 s . co m { const int SIZE = 10; int a[ SIZE ] = { 3, 100, 52, 77, 22, 31, 1, 98, 13, 40 }; vector< int > v( a, a + SIZE ); // copy of a vector< int > v2; ostream_iterator< int > output( cout, " " ); cout << "Vector v before make_heap:\n"; copy( v.begin(), v.end(), output ); make_heap( v.begin(), v.end() ); // create heap from vector v cout << "\nVector v after make_heap:\n" ; copy( v.begin(), v.end(), output ); sort_heap( v.begin(), v.end() ); // sort elements with sort_heap cout << "\nVector v after sort_heap:\n" ; copy( v.begin(), v.end(), output ); cout << endl; }