Pagini recente » Cod sursa (job #2868525) | Cod sursa (job #2781531) | Diferente pentru home intre reviziile 430 si 431 | Cod sursa (job #2680003) | Cod sursa (job #1183869)
#include<fstream>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int n,a[500005],N;
inline void Insert(int x)
{
int aux;
a[++N]=x;
aux=N;
while ((aux>>1)>=1 && x<a[aux>>1])
{
swap(a[aux>>1],a[aux]);
aux>>=1;
}
}
inline void HeapSort(int &c)
{
int son=1,test=0,father=1;
a[1]=a[c];
c--;
while (!test)
{
son=father<<1;
if (son<=n)
{
if (son<n && a[son+1]<a[son]) son++;
if (a[son]<a[father])
{
swap(a[son],a[father]);
father=son;
}
else test=1;
}
else test=1;
}
}
int main()
{
int i,x;
fin>>n;
for (i=1;i<=n;i++)
{
fin>>x;
Insert(x);
}
for (i=1;n;i++)
{
fout<<a[1]<<" ";
HeapSort(n);
}
return 0;
}