Cod sursa(job #1091737)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 25 ianuarie 2014 22:59:07
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;

const int digits = 4;        //Digits
const int r = 8;            //Bits
const int radix = 1 << r;    //Buckets
const int mask = radix - 1;  //BitMask

void RadixSort( vector<int>& a )
{
    const int SIZE = a.size();

    vector <int> b( SIZE );
    vector <int> bucket( radix );

    for ( int i = 0, shift = 0; i < digits; ++i, shift += r )
    {
        for ( int j = 0; j < radix; ++j )
                bucket[j] = 0;

        for ( int j = 0; j < SIZE; ++j )
                ++bucket[ ( a[j] >> shift ) & mask ];

        for ( int j = 1; j < radix; j++ )
                bucket[j] += bucket[j - 1];

        for ( int j = SIZE - 1; j >= 0; --j )
                b[ --bucket[ ( a[j] >> shift ) & mask ] ] = a[j];

        for ( int j = 0; j < SIZE; j++ )
                a[j] = b[j];
    }
}


void read( vector<int>& a )
{
    ifstream f("radixsort.in");

    int N, A, B, C;

    f >> N >> A >> B >> C;

    a.push_back( B );

    for ( int i = 1; i < N; ++i )
    {
        int x = ( 1LL * A * a[i - 1] + B ) % C;

        a.push_back( x );
    }

    f.close();
}

void write( vector<int> a )
{
    ofstream g("radixsort.out");

    for ( unsigned i = 0; i < a.size(); i+=10 )
            g << a[i] << " ";

    g << "\n";

    g.close();
}

int main()
{
    vector<int> v;

    read(v);
    RadixSort(v);
    write(v);

    return 0;
}