Pagini recente » Rating calugar ilinca (ilincutzza2) | Cod sursa (job #2103068) | Cod sursa (job #1321317)
#include <iostream>
#include <fstream>
using namespace std;
#define maxn 500002
#define maxx 2147483647
int n, v[maxn], sol[maxn], np;
void down(int x)
{
int m=0;
while (m!=x)
{
m=x;
if (2*m<=np && v[x]>=v[2*m]) x=2*m;
if (2*m+1<=np && v[2*m+1]<=v[x]) x=2*m+1;
swap(v[m], v[x]);
}
}
void up(int x)
{
while (x/2 && v[x]<=v[x/2])
{
swap(v[x], v[x/2]);
x/=2;
}
}
int main()
{
ifstream f("algsort.in");
ofstream g("algsort.out");
f>>n;
int i;
for (i=1;i<=n;i++)
{
f>>v[i];
up(i);
}
np=n;
for (i=1;i<=n;i++)
{
g<<v[1]<<' ';
swap(v[1], v[np]);
np--;
down(1);
}
}