Cod sursa(job #2136288)

Utilizator inquisitorAnders inquisitor Data 19 februarie 2018 20:12:34
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <cstdio>
#include <queue>

int N, x;

std :: queue<int> bucket1[256], bucket2[256];

int main()
{
    freopen("algsort.in", "r", stdin);
    freopen("algsort.out", "w", stdout);

    scanf("%d", &N);

    for(int i = 0; i < N; ++i)
    {
        scanf("%d", &x);

        bucket1[x & 255].push(x);
    }

    for(int i = 0; i != 256; ++i)
    {
        while(!bucket1[i].empty())
        {
            x = bucket1[i].front(); bucket1[i].pop();

            bucket2[x >> 8 & 255].push(x);
        }
    }

    for(int i = 0; i != 256; ++i)
    {
        while(!bucket2[i].empty())
        {
            x = bucket2[i].front(); bucket2[i].pop();

            bucket1[x >> 16 & 255].push(x);
        }
    }

    for(int i = 0; i != 256; ++i)
    {
        while(!bucket1[i].empty())
        {
            x = bucket1[i].front(); bucket1[i].pop();

            bucket2[x >> 24 & 255].push(x);
        }
    }

    for(int i = 0; i != 256; ++i)
    {
        while(!bucket2[i].empty())
        {
            printf("%d ", bucket2[i].front()); bucket2[i].pop();
        }
    }

    return 0;
}