Cod sursa(job #1168244)

Utilizator andreiagAndrei Galusca andreiag Data 7 aprilie 2014 17:59:30
Problema Radix Sort Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>
#include <algorithm>

using namespace std;
const int Nmax = 10000005;

int a[Nmax];

void mysort(int begin, int end, int k)
{
    int zero = begin, one = end;
    while (zero + 1 < one)
    {
        zero++; one--;
        while(zero < one && !(a[zero] & (1 << k)))
            zero++;
        while(zero < one && (a[one] & (1 << k)))
            one--;
        swap(a[zero], a[one]);
        /*
        if (a[zero] & (1 << k))
            swap(a[zero--], a[--one]);
        */
    }
    if (a[zero] & (1 << k)) zero--;
    if (!(a[one] & (1 << k))) one++;
    if (k >= 3) return;
    if (begin < zero) mysort(begin, zero+1, k-1);
    if (one < end)    mysort(one-1, end, k-1);
}

void insert_sort(int N)
{
    for (int i = 1; i < N; i++)
    {
        int j = i-1, x = a[i];
        while (j >= 0 && a[j] > x)
        {
            a[j+1] = a[j];
            j--;
        }
        a[j+1] = x;
    }
}

int main()
{
    ifstream f ("radixsort.in");
    ofstream g ("radixsort.out");

    int N, A, B, C;
    f >> N >> A >> B >> C;
    
    int x = B;
    for (int i = 0; i < N; i++)
    {
        a[i] = x;
        x = (1ll * A * x + B) % C;
    }

    int mx = 0;
    for (int i = 0; i < 32; i++)
        if (C & (1 << i)) mx = i;

    mysort(-1, N, mx);
    insert_sort(N);

    for (int i = 0; i < N; i++)
        if (i % 10 == 0)
            g << a[i] << ' ';
    g << '\n';

    return 0;
}