Pagini recente » Clasamentul arhivei de probleme | Cod sursa (job #2529216) | Cod sursa (job #2533309) | Cod sursa (job #3175626) | Cod sursa (job #2151987)
#include <fstream>
#include <algorithm>
#define nmax 500002
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int v[nmax],cnt=0;
inline void adauga(int x)
{
v[++cnt]=x;
int cmt=cnt;
while(v[cmt]<v[cmt/2])
swap(v[cmt],v[cmt/2]),cmt>>=1;
}
inline void afis()
{
fout<<v[1]<<" ";
v[1]=v[cnt];
cnt--;
int p=1;
while(p*2<=cnt&&(v[p]>v[p*2]||v[p]>v[p*2+1]))
{
if(v[p*2]<v[p*2+1])
{
swap(v[p],v[p*2]);
p=p*2;
}
else
{
swap(v[p],v[p*2+1]);
p=p*2+1;
}
}
}
int main()
{
int n,nr;
char c;
fin>>n;
c=fin.get();
for(int i=1;i<=n;i++)
{
nr=0;
c=fin.get();
while(c>='0'&&c<='9')
{
nr=nr*10+c-'0';
c=fin.get();
}
adauga(nr);
}
for(int i=1;i<=n;i++)
afis();
return 0;
}