Cod sursa(job #379337)

Utilizator alexandru92alexandru alexandru92 Data 31 decembrie 2009 20:08:47
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 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 s, int e )
{
    int w=2*s+1;
    while( w < e )
    {
        if( w+1 < e && v[w+1] > v[w] )
            ++w;
        if( v[s] >= v[w] )
            return;
        swap( s, w );
        s=w;
        w=2*s+1;
    }
}
void heapify( int e )
{
    for( int v=e/2-1; v >= 0; --v )
        siftdown( v, e );
}
void HeapSort( int s, int e )
{
    heapify(e);
    while( e > 1 )
    {
        --e;
        swap( 0, e );
        siftdown( s, e );
    }
}

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;
}