Pagini recente » Cod sursa (job #1208202) | Cod sursa (job #948429) | Cod sursa (job #1652142) | Cod sursa (job #1296894) | Cod sursa (job #1808627)
#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]) && (2 * j <= n) && (2 * j + 1 <= n))
{
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;
}