Cod sursa(job #2642504)

Utilizator LivcristiTerebes Liviu Livcristi Data 15 august 2020 18:31:57
Problema Radix Sort Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>
#define NUM 10000005
using namespace std;
int v[NUM];
int temp[NUM];
int n, a, b, c;
void ByteSort(int * prim, int * sec, int byte)
{
    int counter[256] = {0};
    int index[256] = {0};

    for(int i = 0; i < n; ++i)
        ++counter[(prim[i] >> byte) & 255];

    for(int i = 1; i < 256; ++i)
        index[i] = index[i - 1] + counter[i - 1];

    for(int i = 0; i < n; ++i)
        sec[index[(prim[i] >> byte) & 255]++] = prim[i];
}
void radixSort()
{
    ByteSort(v, temp, 0);
    ByteSort(temp, v, 8);
    ByteSort(v, temp, 16);
    ByteSort(temp, v, 32);
}
int main()
{
    ifstream f("radixsort.in");
    ofstream g("radixsort.out");

    f >> n >> a >> b >> c;
    v[0] = b;
    for(int i = 1; i < n; ++i)
        v[i] = (1LL * a * v[i - 1] % c + b) % c;

    radixSort();

    for(int i = 0; i < n; i += 10)
        g << v[i] << ' ';
    f.close();
    g.close();
}