Pagini recente » Cod sursa (job #1810156) | Cod sursa (job #1808630)
#include <stdio.h>
#define MAXN 500001
int heap[MAXN];
int main(){
int n, i, m = 0, aux, j, k;
FILE *f, *g;
f=fopen("algsort.in","r");
g=fopen("algsort.out","w");
fscanf(f,"%d",&n);
fscanf(f,"%d",&heap[1]);
for(i = 2;i <= n; ++i){
fscanf(f,"%d",&heap[i]);
while(heap[i] > heap[i / 2]){
aux = heap[i];
heap[i] = heap[i / 2];
heap[i / 2] = aux;
}
}
m = n;
while(n){
aux = heap[1];
heap[1] = heap[n];
heap[n] = aux;
--n;
j = 1;
while(heap[j] < heap[j * 2] || heap[j] < heap[j * 2 + 1])
{
if(heap[j * 2] > heap[j * 2 + 1]){
aux = heap[j];
heap[j] = heap[j * 2];
heap[j * 2] = aux;
j = j * 2;
}
else{
aux = heap[j];
heap[j] = heap[j * 2 + 1];
heap[j * 2 + 1] = aux;
j = j * 2 + 1;
}
}
}
for(i = 1;i <= m; ++i)
fprintf(g,"%d ",heap[i]);
return 0;
}