Cod sursa(job #1167400)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 4 aprilie 2014 22:04:34
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <fstream>
#include <vector>

using namespace std;

const int BIT_COUNT = 8;
const int MAX_STEPS = 4;

vector<int> Values;

void RadixSort(vector<int> &values, const int from, const int to, const int step = MAX_STEPS - 1) {
    if (from >= to - 1 || step < 0)
        return;
    vector< vector<int> > buckets = vector< vector<int> >(1 << BIT_COUNT, vector<int>());
    for (int i = from; i < to; ++i)
        buckets[(values[i] >> (BIT_COUNT * step)) & ((1 << BIT_COUNT) - 1)].push_back(values[i]);
    for (int b = 0, j = 0; b < (1 << BIT_COUNT); ++b) {
        int nextj = j;
        for (vector<int>::const_iterator v = buckets[b].begin(); v != buckets[b].end(); ++v, ++nextj)
            values[from + nextj] = *v;
        RadixSort(values, j, nextj, step - 1);
        j = nextj;
    }
}

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, 0, int(Values.size()));
    Print();
    return 0;
}