Pagini recente » Cod sursa (job #2755490) | Cod sursa (job #3180161) | Cod sursa (job #3230762) | Cod sursa (job #1896511) | Cod sursa (job #2757935)
#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 ;
int fd = 2 * p + 1 ;
int 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;
}