Cod sursa(job #379328)

Utilizator alexandru92alexandru alexandru92 Data 31 decembrie 2009 19:53:59
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on December 31, 2009, 7:28 PM
 */
#include <vector>
#include <fstream>
#include <iterator>

/*
 *
 */
using namespace std;
vector<int> v;
int size_threshold;
inline void swap( int x, int y )
{
    v[x]+=v[y];
    v[y]=v[x]-v[y];
    v[x]-=v[y];
}
void siftdown( int start, int end )
{
    for( int root=start; 2*root+1 <= end; ++root )
    {
        int child=2*root+1;
        if( child+1 < end && v[child] < v[child+1] )
            ++child;
        if( v[root] < v[child] )
        {
            swap( root, child );
            root=child;
        }
        else return;
    }
}
void heapify( int end )
{
    for( int start=(end-2)/2; start >=0; --start )
        siftdown( start, end-1 );
}
void HeapSort( int start, int end )
{
    heapify( end );
    for( end-=1; end > 0; --end )
        swap( 0, end ), siftdown( start, end-1 );
}
int main()
{
    ifstream in("algsort.in");
    in.ignore();
    copy( istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v) );
    ofstream out("algsort.out");
    HeapSort( 0, v.size() );
    copy( v.begin(), v.end(), ostream_iterator<int>(out, " " ) );
    return 0;
}