Cod sursa(job #2592448)

Utilizator RG1999one shot RG1999 Data 1 aprilie 2020 18:22:33
Problema Radix Sort Scor 0
Compilator c-32 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sort_count(int *v, int * sorted, int n, int shft ) {

    int vl;
    int frq = 0;
    int* app = calloc(300, sizeof(int));
    for(int i = 0; i < n; i++) {
        vl  =  v[i] << shft >> 24;
        app[vl]++;
        frq = vl > frq ? vl : frq; 
    }

    for(int i = 0; i <= frq; i++) {
        app[i] += app[i - 1];
    }

    for(int i = 0; i < n; i++) {
        vl  =  v[i] << shft >> 24;
        sorted[app[vl] - 1]  = v[i];
        app[vl]--;
    }

    return;

    

}

int main() {
    int n, a, b, c;
    scanf("%d %d %d %d", &n, &a, &b, &c);
    int v[n], sorted[n];
    v[0] = b;
    int step = b;
    int count = 0;
    for(int i = 0; i < n; i++) {
        if(i % 9 == 0) {
            v[count++] = step;
        }
        step = (a * step + b) % c;
    }
    sort_count(v, sorted, count, 24);
    sort_count(sorted, v, count, 16);
    sort_count(v, sorted, count,  8);
    sort_count(sorted, v, count, 0);
    for(int i = count - 1; i >= 0; i--) {
         printf("%d ", v[i]);
    }
    

}