Pagini recente » Cod sursa (job #2853699) | Cod sursa (job #2550101) | Cod sursa (job #2533490) | Cod sursa (job #74215) | Cod sursa (job #980059)
Cod sursa(job #980059)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <list>
using namespace std;
#define Hmax 500005
struct nod
{
int val;
int ind;
};
struct cmp {
bool operator() (nod &a, nod &b) {
return a.val > b.val;
}
};
struct NODE
{
list<int> l;
} lista[22];
int lg2( int key )
{
int lg = -1;
while( key )
{
lg++;
key >>= 1;
}
return lg;
}
void AlexSort( int *v, int N )
{
int log2 = lg2( N );
int NR = ( N % log2 == 0 ? N / log2 : N / log2 + 1 );
int ind = 1;
for ( int i = 1; i <= NR; ++i )
{
for ( int j = 0; j < log2; ++j )
{
if ( ind > N )
break;
lista[i].l.push_back ( v[ ind++ ]);
}
}
for ( int i = 1; i <= NR; ++i )
{
lista[i].l.sort();
}
priority_queue <nod, vector <nod>, cmp> heap;
for ( int i = 1; i <= NR; i++ )
{
nod x;
x.val = lista[i].l.front();
x.ind = i;
lista[i].l.pop_front();
heap.push( x );
}
int index = 1;
while( !heap.empty() )
{
v[ index++ ] = heap.top().val;
ind = heap.top().ind;
heap.pop();
if ( lista[ind].l.size() > 0 )
{
nod x;
x.val = lista[ind].l.front();
x.ind = ind;
lista[ind].l.pop_front();
heap.push( x );
}
}
}
int main()
{
int a[Hmax], n;
ifstream f("algsort.in");
ofstream g("algsort.out");
f >> n;
for ( int i = 1; i <= n; i++ )
f >> a[i];
AlexSort(a,n);
for ( int i = 1; i <= n; i++ )
g<<a[i]<<" ";
return 0;
}