Pagini recente » Cod sursa (job #1226676) | Cod sursa (job #673699) | Cod sursa (job #1241297) | Cod sursa (job #2478475) | Cod sursa (job #1816371)
#include <fstream>
using namespace std;
ifstream cin ("algsort.in");
ofstream cout ("algsort.out");
int n,v[500010];
void climb (int poz)
{ int val=v[poz];
while(3>2)
{
if(v[poz/2]>val && poz>1)
v[poz]=v[poz/2],poz/=2;
else { v[poz]=val; break; }
}
}
void read ()
{
cin>>n; int x;
for(int i=1;i<=n;i++)
{
cin>>x;
v[i]=x;
climb(i);
}
}
void down (int poz,int m)
{
int val=v[poz],fiu;
while(3>2)
{ fiu=0;
if(poz*2<=m) fiu=poz*2;
if(poz*2+1<=m && v[poz*2+1]<v[fiu]) fiu=2*poz+1;
if(fiu!=0 && val>v[fiu])
v[poz]=v[fiu],poz=fiu;
else { v[poz]=val; break; }
}
}
void sortare ()
{
for(int i=1;i<=n;i++)
{
cout<<v[1]<<" ";
v[1]=v[n-i+1];
down(1,n-i);
}
}
int main()
{
read();
sortare();
cin.close();
cout.close();
return 0;
}