Pagini recente » Cod sursa (job #2330178) | Cod sursa (job #511425) | Cod sursa (job #2712763) | Cod sursa (job #3212171) | Cod sursa (job #1262333)
#include <fstream>
using namespace std;
ifstream fin ("algsort.in");
ofstream fout ("algsort.out");
int h[500010],n,i,x,p,c,aux,k;
void insert (int x) {
h[++k]=x;
c=k;
p=k/2;
while (p>0) {
if (h[p]>h[c]) {
aux= h[c];
h[c]=h[p];
h[p]=aux;
c=p;
p/=2;
}else
break;
}
}
void delete_root() {
h[1]=h[k];
k--;
p=1;
c=2;
while (c <= k) {
if (c+1<=k && h[c+1]<h[c])
c++;
if (h[p]>=h[c]){
aux=h[c];
h[c]=h[p];
h[p]=aux;
p=c;
c*=2;
}else
break;
}
}
int main () {
fin>>n;
for (i=1;i<=n;i++){
fin>>x;
insert (x);
}
for (i=1;i<=n;i++) {
fout<<h[1]<<" ";
delete_root();
}
return 0;
}