Tutorialspoint.dev

sort_heap function in C++

The sort_heap( ) is an STL algorithm which sorts a heap within the range specified by start and end. Sorts the elements in the heap range [start, end) into ascending order.

The second form allows you to specify a comparison function that determines when one element is less than another.
Defined in header

It has two versions, which are defined below:.
1. Comparing elements using “<":
Syntax:

template 
void sort_heap(RandIter start, RandIter end);
start, end :     the range of elements to sort
Return Value:  Since, return type is void, so it doesnot return any value. 

Implementation

template
void sort_heap( RandIter start, RandIter end );
{
    while (start != end)
        std::pop_heap(start, end--);
}

2. By comparing using a pre-defined function:
Syntax:



template 
void sort_heap(RandIter start, RandIter end, Comp cmpfn);
start, end :    the range of elements to sort
comp:   comparison function object (i.e. an object that satisfies the requirements of Compare) 
which returns ?true if the first argument is less than the second.
Return Value : Since, its return type is void, so it doesnot return any value. 

Implementation

template
void sort_heap( RandIter start, RandIter end, Comp cmpfn );
{
    while (start != end)
        std::pop_heap(start, end--, cmpfn);
}
// CPP program to illustrate
// std::sort_heap
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    vector<int> v = {8, 6, 2, 1, 5, 10}; 
   
    make_heap(v.begin(), v.end());
   
    cout << "heap:   ";
    for (const auto &i : v) {
     cout << i << ' ';
    }   
   
    sort_heap(v.begin(), v.end());
   
    std::cout <<endl<< "now sorted:   ";
    for (const auto &i : v) {                                                   
        cout << i << ' ';
    }   
    std::cout <<endl;
}

/div>

Output:

heap: 10 6 8 1 5 2 
now sorted: 1 2 5 6 8 10 

Another Example :

// CPP program to illustrate
// std::sort_heap
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
  
int main( ) {
   using namespace std;
   vector <int> vt1, vt2;
   vector <int>::iterator Itera1, Itera2;
  
   int i;
   for ( i = 1 ; i <=5 ; i++ )
      vt1.push_back( i );
  
   random_shuffle( vt1.begin( ), vt1.end( ) );
  
   cout << "vector vt1 is ( " ;
   for ( Itera1 = vt1.begin( ) ; Itera1 != vt1.end( ) ; Itera1++ )
      cout << *Itera1 << " ";
   cout << ")" << endl;
  
   sort_heap (vt1.begin( ), vt1.end( ) );
   cout << "heap vt1 sorted range: ( " ;
   for ( Itera1 = vt1.begin( ) ; Itera1 != vt1.end( ) ; Itera1++ )
      cout << *Itera1 << " ";
   cout << ")" << endl;
}
    

Output:

vector vt1 is ( 5 4 2 3 1 )
heap vt1 sorted range: ( 1 2 3 4 5 )

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter