Cod sursa(job #1540794)

Utilizator ManDark97Melinte Tudor-Matei ManDark97 Data 3 decembrie 2015 11:43:40
Problema Radix Sort Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.88 kb
#include <iostream>
#include<fstream>
using namespace std;
ifstream in("radixsort.in");
ofstream out("radixsort.out");
long long unsigned getMax(long long unsigned arr[], long long unsigned n)
{
    long long unsigned mx = arr[0];
    for (long long unsigned i = 1; i < n; i++)
        if (arr[i] > mx)
            mx = arr[i];
    return mx;
}
void countSort(long long unsigned arr[], long long unsigned n, long long unsigned exp)
{
    long long unsigned output[n]; // output array
    long long unsigned i, count[10] = {0};

    // Store count of occurrences in count[]
    for (i = 0; i < n; i++)
        count[ (arr[i]/exp)%10 ]++;

    // Change count[i] so that count[i] now contains actual
    //  position of this digit in output[]
    for (i = 1; i < 10; i++)
        count[i] += count[i - 1];

    // Build the output array
    for (i = n - 1; i >= 0; i--)
    {
        output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
        count[ (arr[i]/exp)%10 ]--;
    }

    // Copy the output array to arr[], so that arr[] now
    // contains sorted numbers according to current digit
    for (i = 0; i < n; i++)
        arr[i] = output[i];
}

// The main function to that sorts arr[] of size n using
// Radix Sort
void radixsort(long long unsigned arr[], long long unsigned n)
{
    // Find the maximum number to know number of digits
    long long unsigned m = getMax(arr, n);

    // Do counting sort for every digit. Note that instead
    // of passing digit number, exp is passed. exp is 10^i
    // where i is current digit number
    for (long long unsigned exp = 1; m/exp > 0; exp *= 10)
        countSort(arr, n, exp);
}

int main()
{    long long unsigned a,b,c,i,n,v[100000000];
    in>>n>>a>>b>>c;
    v[0]=b;
    for(i=1;i<n;i++)
    {
        v[i]=(a*v[i-1]+b)%c;
    }
radixsort(v,n);
for(i=0; i<n; i++)
        out<<v[i]<<" ";
    return 0;
}