Pagini recente » Istoria paginii runda/oni2014_z1_ix/clasament | Cod sursa (job #352590)
Cod sursa(job #352590)
/*
* File: main.cpp
* Author: speedemon
*
* Created on October 2, 2009, 2:40 PM
*/
#include <vector>
#include <fstream>
#include <cstdlib>
#include <iterator>
/*
*
*/
using namespace std;
ifstream in;
ofstream out;
vector< int > v;
inline void swap( int a, int b )
{
int aux=v[a];
v[a]=v[b];
v[b]=aux;
}
void downheap( int start, int end )
{
for( int root=2*start+1; root < end; )
{
if( root+1 < end && v[root+1] > v[root] ) ++root;
if( v[start] > v[root] ) return;
swap( start, root );
start=root;
root=2*start+1;
}
}
void make_heap( int end )
{
for( int start=end/2-1; start >= 0; --start )
downheap( start, end );
}
void HeapSort( int end )
{
make_heap( end );
for( --end; end >= 0; --end )
{
swap( 0, end );
downheap( 0, end );
}
}
int main()
{int N,x;
in.open("algosort.in");
in>>N;
while( N-- )
{
in>>x;
v.push_back(x);
}
HeapSort( v.size() );
out.open("algosort.out");
copy( v.begin(), v.end(), ostream_iterator<int>( out, " ") );
return EXIT_SUCCESS;
}