Cod sursa(job #2609420)

Utilizator darksky185Alexandru Gabriel darksky185 Data 2 mai 2020 16:57:50
Problema Radix Sort Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
#include <iostream>

using namespace std;

int v[10000000];
int aux[10000000];

void radix_sort(const int n) {
    const int NB = 16;

    const int B = 1 << NB;

    int nr[B], poz[B];

    for(int k = 0; k < 2; ++k) {

        int KNB = k*NB;

        for(int i = 0; i < B; ++i) {
            nr[i] = 0;
        }

        for(int j = 0; j < n; ++j) {
            ++nr[(v[j]>>KNB)&(B-1)];
        }

        poz[0] = 0;
        for(int i = 1; i < B; ++i) {
            poz[i] = poz[i-1] + nr[i-1];
        }
        for(int j = 0; j < n; ++j) {
            aux[poz[(v[j]>>KNB)&(B-1)]++] = v[j];
        }
        for(int j = 0; j < n; ++j) {
            v[j] = aux[j];
        }
    }

}

void generare_nr(int n, int a, int b, int c) {
    v[0] = b;
    for(int i = 1; i < n; ++i) {
        v[i] = ((long long)a * v[i-1] + b) % c;
    }
}

int main() {

    ifstream fin("radixsort.in");
    ofstream fout("radixsort.out");

    int n, a, b, c;

    fin >> n >> a >> b >> c;

    generare_nr(n, a, b, c);

    radix_sort(n);

    for(int i = 0; i < n; i += 10) {
        fout << v[i] << " ";
    }

    fin.close();
    fout.close();

    return 0;
}