Cod sursa(job #2823990)

Utilizator mircea_007Mircea Rebengiuc mircea_007 Data 30 decembrie 2021 14:34:11
Problema Radix Sort Scor 30
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.33 kb
// This program was written on 30.12.2021
// for problem radixsort
// by Mircea Rebengiuc

#include <stdio.h>
#include <ctype.h>

#define MAXN 10000000
#define STEP 10
#define SIGMA 16 // vom lua numerele in baza 16
#define LSIGMA 4 // log2(sigma)
#define MASK (SIGMA - 1)
#define NUMSTEP 8 // 8 * 4 = 32 biti
#define NIL -1

int v[MAXN];
int current[MAXN];
int ord[MAXN];

int list[SIGMA];
int next[MAXN];

int main(){
  FILE *fin  = fopen("radixsort.in",  "r");
  FILE *fout = fopen("radixsort.out", "w");
  
  int n, i, a, b, c, step;
  
  fscanf(fin, "%d%d%d%d", &n, &a, &b, &c);
  
  current[0] = v[0] = b;
  ord[0] = 0;
  for( i = 1 ; i < n ; i++ ){
    current[i] = v[i] = (((long long)a) * v[i - 1] + b) % c;
    ord[i] = i;
  }
  
  for( step = 0 ; step < STEP ; step++ ){
    for( c = 0 ; c < SIGMA ; c++ )
      list[c] = NIL;
  
    for( i = 0 ; i < n ; i++ ){
      c = (current[ord[i]] & MASK);
      next[ord[i]] = list[c];
      list[c] = ord[i];
    }

    // sortare stabila (bucket sort)    
    a = n;
    for( c = SIGMA ; c-- ; ){
      i = list[c];
      while( i != NIL ){
        ord[--a] = i;
        i = next[i];
      }
    }
    
    for( i = 0 ; i < n ; i++ )
      current[i] >>= LSIGMA;
  }
  
  for( i = 0 ; i < n ; i += STEP )
    fprintf(fout, "%d ", v[ord[i]]);
  fputc('\n', fout);
  
  fclose(fin);
  fclose(fout);
  return 0;
}