Cod sursa(job #1041947)

Utilizator dan.ghitaDan Ghita dan.ghita Data 26 noiembrie 2013 13:28:58
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
int n=0, v[10000], m, p, x;
vector<int> out;
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(){
    //cout<<n<<'\n';
//out.push_back(v[1]);
g<<v[1]<<' ';
swap(v[1], v[n]); n--;
p=1;
while(2*p<=n){
if(2*p+1>n){
    if(v[p]>v[2*p]) swap(v[p], v[2*p]);
    break;
}
else{
if(v[p]>v[2*p]&&v[2*p]<v[2*p+1]) swap(v[p], v[2*p]), p=p*2;
else if(v[p]>v[2*p+1]&&v[2*p]>v[2*p+1]) swap(v[p], v[2*p+1]), p=p*2+1;
}

}
//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; //cout<<x;
        push(x);
    }
    for(int i=1; i<=m; ++i)
        pop();
        //cout<<v[i]<<' ';
    return 0;
}