Cod sursa(job #2592472)

Utilizator RG1999one shot RG1999 Data 1 aprilie 2020 18:41:05
Problema Radix Sort Scor 0
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.26 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 / 10], sorted[n / 10];
    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, 0);
    sort_count(sorted, v, count, 1);
    sort_count(v, sorted, count,  2);
    sort_count(sorted, v, count, 3);
    for(int i = count - 1; i >= 0; i--) {
         fprintf(g, "%d ", v[i]);
    }
    fclose(g);
    fclose(f);
    
 
}