Cod sursa(job #1041985)

Utilizator dan.ghitaDan Ghita dan.ghita Data 26 noiembrie 2013 13:51:00
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int n=0, v[500005], m, p, x;
void push(int x){
v[++n]=x; //cout<<v[n];
int p=n;
while(v[p]<v[p/2]&&p>1)
    swap(v[p], v[p/2]), p/=2;

}

void pop(){
g<<v[1]<<' ';
swap(v[1], v[n--]);
p=1;
int s;
while(2*p<=n){
s=2*p;
if(s+1>n) v[s+1]=2147483647;
if(v[p]>v[s]&&v[s]<v[s+1]) swap(v[p],v[s]);
else if(v[p]>v[s+1]&&v[s]>v[s+1]) swap(v[p],v[s+1]), ++s;
p=s;
}


//for(int i=1; i<=n; ++i) cout<<v[i]<<' '; cout<<'\n';
}
int main()
{
    f>>m;
    for(int i=1; i<=m; ++i){
        f>>x;
        push(x);
    }
    for(int i=1; i<=m; ++i)
        pop();

    return 0;
}