Cod sursa(job #1451785)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 18 iunie 2015 15:10:48
Problema Sortare prin comparare Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>

#define DIM 3666013
#define MASK 255

using namespace std;
vector<int> V;

char buffer[DIM];
int poz = DIM - 1;

void Read(int &A){
    A = 0;
    while('0' > buffer[poz] || buffer[poz] > '9')
        if(++poz == DIM)
            fread(buffer,1,DIM,stdin),poz = 0;
    while('0' <= buffer[poz] && buffer[poz] <= '9')
    {
        A = A * 10 + buffer[poz] - 48;
        if(++poz == DIM)
            fread(buffer,1,DIM,stdin),poz = 0;
    }
}

void Radix(int k)
{
    vector<int> bucket[MASK];
    for(auto it : V)
        bucket[MASK&(it>>k)].push_back(it);
    int p = 0;
    for(int i = 0; i < MASK; ++i)
        for(auto it : bucket[i])
            V[p++] = it;
}

void Sort()
{
    for(int i = 0; i <= 3; ++i)
        Radix(8*i);
}

int main()
{
    freopen("algsort.in","r",stdin);
    freopen("algsort.out","w",stdout);
    int N;
    Read(N);
    V.resize(N);
    for(int i = 0; i < N; ++i)
        Read(V[i]);
    Sort();
    for(auto it : V)
        printf("%d ",it);

    return 0;
}