Pagini recente » Cod sursa (job #1076702) | Cod sursa (job #1856675) | Cod sursa (job #935712) | Cod sursa (job #1845437) | Cod sursa (job #266864)
Cod sursa(job #266864)
#include <stdio.h>
#define nmax 500001
long int A[nmax],n;
void shift(int x)
{
int ok=1,l;
while (ok)
{
ok=0;
if (2*x<=n) l=2*x;
if (2*x+1<=n) if (A[2*x]>A[2*x+1]) l = 2*x+1;
if (A[x]>A[l])
{
ok=1;
A[0]=A[x];
A[x]=A[l];
A[l]=A[0];
x=l;
}
}
}
int main()
{
freopen("algsort.in","r",stdin);
freopen("algsort.out","w",stdout);
long int i,k;
scanf("%ld",&n);
for (i=1;i<=n;i++) scanf("%ld",&A[i]);
for (i=n/2;i;i--)
shift(i);
k=n;
for (i=1;i<=k;i++)
{
printf("%ld ",A[1]);
A[1]=A[n];
n--;
shift(1);
}
return 0;
}