Cod sursa(job #1966251)

Utilizator nicu_serteSerte Nicu nicu_serte Data 15 aprilie 2017 01:26:58
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("radixsort.in");
ofstream fout("radixsort.out");
#define nmax 10000005
int n, v[nmax];
void citire()
{
    int i, a, b, c;
    fin>>n>>a>>b>>c;
    fin.close();
    v[1]=b;
    for(i=2; i<=n; i++)
        v[i]=(1LL*a*v[i-1]+b)%c;
}
void afisare()
{
    int i;
    for(i=1; i<=n; i+=10)
        fout<<v[i]<<' ';
}
int key(int x, int sh)
{
    return ((x>>8*sh)&((1<<8)-1));
}
void sortare(int sh)
{
    int i, k;
    vector<int> bucket[1<<8];
    vector<int>::iterator it;
    for(i=1; i<=n; i++)
        bucket[key(v[i], sh)].push_back(v[i]);
    k=0;
    for(i=0; i<=(1<<8)-1; i++)
        for(it=bucket[i].begin(); it!=bucket[i].end(); it++)
            v[++k]=*it;
}
void radix()
{
    int i;
    for(i=0; i<4; i++)
        sortare(i);
}
int main()
{
    citire();
    radix();
    afisare();
    return 0;
}