Cod sursa(job #439226)

Utilizator alexandru92alexandru alexandru92 Data 11 aprilie 2010 14:18:03
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
/* 
 * 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;
}