Cod sursa(job #2757934)

Utilizator tubac_vladTubac Vlad-Cristian tubac_vlad Data 7 iunie 2021 17:59:25
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");

const int N=2e5+10;
int v[N], nh, n ;

void coboara(int p)
{
    int fs =2 * p ;
    fd = 2 * p + 1 ;
    bun = p ;
    if( fs <= nh && v[fs] > v[bun])
    {
        bun = fs ;
    }
    if( fd <= nh && v[fd] > v[bun] )
    {
        bun=fd ;
    }
    if( bun != p )
    {
        swap( v[bun] ,v[p] ) ;
        coboara( bun ) ;
    }
}
void urca(int p)
{
    while( p>1 && v[p] > v[p/2] )
    {
        swap( v[p], v[p/2] ) ;
        p = p / 2 ;
    }
}
void adauga( int val )
{
    v[++nh] = val ;
    urca(nh) ;
}
int main()
{
    f >> n ;
    nh = 0 ;
    for( int i = 1; i <= n; i++ )
    {
        int aux ;
        f >> aux ;
        adauga(aux);
    }
    for( int i = n ; i > 1 ; i-- )
    {
        swap( v[1], v[i] ) ;
        nh-- ;
        coboara(1) ;
    }
    for(int i = 1; i <= n; i++ )
    {
        g << v[i] << " " ;
    }
    return 0;