Cod sursa(job #2592484)

Utilizator RG1999one shot RG1999 Data 1 aprilie 2020 18:53:07
Problema Radix Sort Scor 0
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.19 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] >> (8*shft)) & 0xff;
        app[vl]++;
        frq = vl > frq ? vl : frq; 
    }
 
    for(int i = 1; i <= frq; i++) {
        app[i] += app[i - 1];
    }
 
    for(int i = 0; i < n; i++) {
        vl = (v[i] >> (8*shft)) & 0xff;
        sorted[app[vl] - 1]  = v[i];
        app[vl]--;
    }
    free(app);
    return;
 
    
 
}
 
int main() {
    int n, a, b, c;
    FILE *f = fopen("radixsort.in", "r");
    FILE *g = fopen("radixsort.out", "w");
    fscanf(f, "%d %d %d %d", &n, &a, &b, &c);
    int v[n], sorted[n];
    v[0] = b;
    int step = b;
    int count = 0;
    v[0] = b;
    for(int i = 1; i < n; i++) {
        v[i] = (1LL * a * v[i - 1] + b) % c;
    }
    sort_count(v, sorted, n, 0);
    sort_count(sorted, v, n, 1);
    sort_count(v, sorted, n,  2);
    sort_count(sorted, v, n, 3);
    for(int i = n - 1; i >= 0; i-=10) {
         fprintf(g, "%d ", v[i]);
    }
    fclose(g);
    fclose(f);
    
 
}