Pagini recente » Cod sursa (job #745843) | Cod sursa (job #251146) | Cod sursa (job #737846) | Cod sursa (job #1351818) | Cod sursa (job #1781876)
#include <bits/stdc++.h>
using namespace std;
int h[500005],n,hs;
void up(int pos){
if(pos==1) return;
if(h[pos/2]<=h[pos]) return;
swap(h[pos],h[pos/2]);
up(pos/2);
}
void down(int pos){
if(2*pos>hs) return;
if(2*pos==hs){
if(h[pos]<=h[2*pos]) return;
swap(h[pos],h[2*pos]);
}
else{
if(h[pos]<=h[2*pos] && h[pos]<=h[2*pos+1]) return;
int fiu;
if(h[2*pos+1]>=h[2*pos]) fiu=2*pos;
else fiu=2*pos+1;
swap(h[pos],h[fiu]);
down(fiu);
}
}
void del(int pos){
if(pos==hs) {
hs--;
return;
}
swap(h[pos],h[hs]);
hs--;
if(h[pos]<h[pos/2]) up(pos);
else down(pos);
}
int main()
{
ifstream in("algsort.in");
ofstream out("algsort.out");
in >> n;
for(hs=1;hs<=n;hs++) {
in >> h[hs];
up(hs);
}
hs--;
for(int i=1;i<=n;i++) {
//for(int j=1;j<=hs;j++) cout << h[j] << ' ';
//cout << '\n';
out << h[1] << ' ';
del(1);
}
}