Cod sursa(job #2126266)

Utilizator TudorVersoiuVersoiu Tudor Sorin TudorVersoiu Data 9 februarie 2018 14:03:09
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.64 kb
#include <iostream>
#include <fstream>
#include <queue>

using namespace std;
ifstream f("radixsort.in" );
ofstream g("radixsort.out");


// C++ implementation of Radix Sort
#include<iostream>
using namespace std;

// A function to do counting sort of arr[] according to
// the digit represented by exp.
void countSort(int arr[], int n, int exp)
{
    int output[n]; // output array
    int 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

int N, cmax;
int v[10000003];

int NrCif(int x) {
    int ncif = 0;
    while ( x ) {
        x/=10;
        ncif++;
    }
    return ncif;
}
void GenerateArray()
{
    int A, B, C;
    f >> N >> A >> B >> C;

    v[1] = B;
    for ( int i=2; i<=N; i++ ) {
        v[i] = (A * v[i-1] + B) % C;
        cmax = max(cmax, v[i]);
    }
}

void RadixSort(int arr[], int n) {
    for (int exp = 1; cmax/exp > 0; exp *= 10)
        countSort(arr, n, exp);
}
int main()
{
    GenerateArray();
    RadixSort(v+1, N);

    for ( int i=0; i*10+1<=N; i++ )
        g << v[i*10+1] << ' ';
}