Pagini recente » Cod sursa (job #551281) | Cod sursa (job #993621) | Cod sursa (job #3293870) | Cod sursa (job #89500) | Cod sursa (job #439226)
Cod sursa(job #439226)
/*
* File: main.cpp
* Author: VirtualDemon
*
* Created on April 11, 2010, 2:08 PM
*/
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
/*
*
*/
using namespace std;
vector< int > v;
inline void DownHeap( int from, int to )
{
for( int son=2*from; son < to; from=son, son=2*from )
{
if( son < to-1 && v[son+1] > v[son] )
++son;
if( v[from] >= v[son] )
return;
swap( v[from], v[son] );
}
}
inline void HeapSort( int from, int to )
{
int i;
for( i=(to-from)/2; i >= 0; --i )
DownHeap( i, to );
while( to > from )
{
swap( v[from], v[to-1] );
--to;
DownHeap( from, to );
}
}
int main(int argc, char** argv)
{
int N;
ifstream in( "algsort.in" );
in>>N;
copy( istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v) );
HeapSort( 0, N );
ofstream out( "algsort.out" );
copy( v.begin(), v.end(), ostream_iterator<int>( out, " " ) );
out<<'\n';
return EXIT_SUCCESS;
}