Cod sursa(job #2509061)

Utilizator bullseYeIacob Sergiu bullseYe Data 13 decembrie 2019 18:54:44
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>
#define DMAX 10000000 + 1

using namespace std;

int numbers[DMAX];
int n, a, b, c;

void citire();
void solve();
void afisare();
inline int getByte(const int &, const int &);
void radixSort(int *, int *, const int &);

int main()
{
    citire();
    solve();
    afisare();
    return 0;
}

void afisare()
{
    ofstream fout("radixsort.out");
    for (int i = 0; i < n; i += 10)
        fout << numbers[i] << ' ';
    fout << '\n';
    fout.close();
}

void radixSort(int *from, int *to, const int &byte)
{
    int buckets[1 << 8];
    memset(buckets, 0, sizeof(buckets));
    for (int i = 0; i < n; ++i)
        ++buckets[getByte(from[i], byte)];

    for (int i = 1; i < (1 << 8); ++i)
        buckets[i] += buckets[i - 1];

    for (int i = n - 1; i >= 0; --i)
    {
        --buckets[getByte(from[i], byte)];
        to[buckets[getByte(from[i], byte)]] = from[i];
    }
}

void solve()
{
    // sortez dupa bytes
    int no_bytes = sizeof(numbers[0]);
    int copy[DMAX];

    for (int i = 0; i < no_bytes; ++i)
        if (i % 2 == 0)
            radixSort(numbers, copy, i);
        else
            radixSort(copy, numbers, i);
}

inline int getByte(const int &nr, const int &byte)
{
    return (nr >> (byte * 8)) & ((1 << 8) - 1);
}

void citire()
{
    ifstream fin("radixsort.in");
    fin >> n >> a >> b >> c;

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

    fin.close();
}