Cod sursa(job #1167757)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 5 aprilie 2014 21:15:23
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#include <vector>

using namespace std;

const int BIT_COUNT = 8;
const int MASK = (1 << BIT_COUNT) - 1;
const int MAX_BITS = 32;

vector<int> Values;

void RadixSort(vector<int> &values, const int shift = 0) {
    if (shift >= MAX_BITS)
        return;
    vector<int> buckets[MASK + 1];
    for (int i = 0; i < int(values.size()); ++i)
        buckets[(values[i] >> shift) & MASK].push_back(values[i]);
    for (int b = 0, i = 0; b <= MASK; ++b)
        for (vector<int>::const_iterator v = buckets[b].begin(); v != buckets[b].end(); ++v)
            values[i++] = *v;
    RadixSort(values, shift + BIT_COUNT);
}

void Read() {
    ifstream cin("radixsort.in");
    int n, a, b, c;
    cin >> n >> a >> b >> c;
    Values = vector<int>(n);
    Values[0] = b;
    for (int i = 1; i < n; ++i)
        Values[i] = (1LL * Values[i - 1] * a + b) % c;
    cin.close();
}

void Print() {
    ofstream cout("radixsort.out");
    for (int i = 0; i < int(Values.size()); i += 10)
        cout << Values[i] << " ";
    cout << "\n";
    cout.close();
}

int main() {
    Read();
    RadixSort(Values);
    Print();
    return 0;
}