Pagini recente » Cod sursa (job #1966971) | Cod sursa (job #270671) | Cod sursa (job #490254) | Cod sursa (job #1875866) | Cod sursa (job #660635)
Cod sursa(job #660635)
#include <cstdio>
#define MAX 500001
int a[MAX],count;
void swap(int x,int y)
{
int aux=a[x];
a[x]=a[y];
a[y]=aux;
}
void siftDown(int a[],int start,int end)
{ int root,child,sss;
root=start;
while(root*2+1<=end)
{
child=root*2+1;
sss=root;
if (a[sss]<a[child])
sss=child;
if(child+1<=end && a[sss]<a[child+1])
sss=child+1;
if (sss!=root)
{ swap(root,sss);
root=sss;
}
else return;
}
}
void heapify(int a[],int count)
{ int start;
start=count/2-1;
while(start>=0)
{
siftDown(a,start,count-1);
start=start-1;
}
}
void heapsort(int a[],int count)
{ int end;
heapify(a,count);
end=count-1;
while(end>0)
{ swap(end,0);
end=end-1;
siftDown(a,0,end);}
}
int main()
{
freopen("algsort.in","r",stdin);
freopen("algsort.out","w",stdout);
scanf("%d",&count);
for(int i=0;i<count;++i)
scanf("%d",&a[i]);
heapsort(a,count);
for(int j=0;j<count;++j)
printf("%d ",a[j]);
return 0;
}