función sort_heap en C++

sort_heap () es un algoritmo STL que ordena un montón dentro del rango especificado por inicio y final. Ordena los elementos en el rango del montón [inicio, final) en orden ascendente.

La segunda forma le permite especificar una función de comparación que determina cuándo un elemento es menor que otro.
Definido en el encabezado

Tiene dos versiones, que se definen a continuación: .
1. Comparación de elementos usando “<«:
Sintaxis:

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. 

Implementación

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

2. Comparando usando una función predefinida:
Sintaxis:

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. 

Implementación

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;
}

Producción:

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

Otro ejemplo :

// 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;
}
    

Producción:

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

Este artículo es una contribución de Shivani Ghughtyal . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a contribuya@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.

Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *