Pagini recente » Cod sursa (job #1724803) | Cod sursa (job #1304337) | Cod sursa (job #2842241) | Cod sursa (job #2044509) | Cod sursa (job #1916755)
#include <fstream>
using namespace std;
ifstream cin ("algsort.in");
ofstream cout ("algsort.out");
int heap[500010],w;
int n;
void climb (int poz)
{
while(3>2)
{
if(poz>1 && heap[poz]<heap[poz/2])
swap(heap[poz],heap[poz/2]),poz/=2;
else break;
}
}
void down (int poz)
{
while(3>2)
{
int fiu=0;
if(poz*2<=w) fiu=poz*2;
if(fiu+1<=w && heap[fiu+1]<heap[fiu]) fiu++;
if(fiu && heap[poz]>heap[fiu])
swap(heap[poz],heap[fiu]),poz=fiu;
else break;
}
}
void read ()
{ int c;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>c;
heap[++w]=c;
climb(w);
}
while(w)
{
cout<<heap[1]<<" ";
heap[1]=heap[w--];
down(1);
}
}
int main()
{
read();
cin.close();
cout.close();
return 0;
}