Pagini recente » Diferente pentru info-oltenia-2018/individual intre reviziile 11 si 10 | Cod sursa (job #1523253) | Cod sursa (job #1553249) | Cod sursa (job #91691) | Cod sursa (job #353433)
Cod sursa(job #353433)
/*
* File: main.cpp
* Author: speedemon
*
* Created on October 2, 2009, 7:09 PM
*/
#include <fstream>
#include <cstdlib>
#include <cmath>
#define InFile "algsort.in"
#define OutFile "algsort.out"
/*
*
*/
using namespace std;
ifstream in;
ofstream out;
unsigned *v;
/** Heap Sort **/
inline void swap( unsigned& a, unsigned& b )
{
a+=b;
b=a-b;
a-=b;
}
void downheap( unsigned start, unsigned end )
{
for( unsigned root=2*start+1; root < end; )
{
if( root+1 < end && v[root] < v[root+1] ) ++root;
if( v[root] <= v[start] ) return;
swap( v[root], v[start] );
start=root;
root=2*start+1;
}
}
void HeapSort( unsigned start, unsigned end )
{
for( int w=end/2-1; w >= 0; -- w ) //make a heap from v
downheap( w, end );
for( end=end-1; end > start; --end )
{
swap( v[0], v[end] );
downheap( 0, end );
}
}
int main()
{unsigned N,i;
in.open(InFile);
in>>N;
v=new unsigned[N];
for( i=0; i < N; ++i ) in>>v[i];
HeapSort( 0, N );
out.open(OutFile);
for( i=0; i < N; ++i ) out<<v[i]<<' ';
delete[] v;
return EXIT_SUCCESS;
}