Pagini recente » Cod sursa (job #3273337) | Cod sursa (job #2657498) | Cod sursa (job #2887102) | Cod sursa (job #1686007) | Cod sursa (job #2174626)
#include <fstream>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int v[500010];
int n=0,m;
void add(int nr){
v[++n] = nr;
int pos = n;
while(pos != 1 && v[pos/2] > v[pos]){
swap(v[pos/2],v[pos]);
pos /= 2;
}
}
void destroy(int pos){
swap(v[1],v[n]);
n--;
int mx = v[1],best=0;
while(pos <= n){
mx = v[pos];
best = 0;
if(pos*2 <= n && mx > v[pos*2]){
best = 1;
mx = v[pos*2];
}
if(pos*2+1 <= n && mx > v[pos*2+1]){
best = 2;
mx = v[pos*2+1];
}
if(!best) break;
else if(best == 1) swap(v[pos],v[pos*2]),pos*=2;
else swap(v[pos],v[pos*2+1]),pos=pos*2+1;
}
}
int main(){
fin>>m;
int i,nr;
for(i = 1; i <= m; i++){
fin>>nr;
add(nr);
}
for(i = 1; i <= m; i++){
fout<<v[1]<<" ";
destroy(1);
}
nr = 1;
}