Pagini recente » Istoria paginii utilizator/hgdsjagjsdhajhgsda | Monitorul de evaluare | Cod sursa (job #1122806) | Istoria paginii utilizator/pitcovicinatalia | Cod sursa (job #2077543)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void radix_sort (int* array, int n, int byte)
{
int mask = 0xff;
int i, r = 256;
int* count = calloc(r+1, sizeof(int));
int* aux = calloc(n, sizeof(int));
for (i = 0; i < n; i++) {
int key = (array[i] >> (byte * 8)) & mask;
count[key+1]++; // frequency vector
}
for (i = 0; i < r; i++)
count[i+1] = count[i+1] + count[i]; // transf keys into indexes
for (i = 0; i < n; i++) {
unsigned int key = (array[i] >> (byte * 8)) & mask;
aux[count[key]] = array[i];
count[key]++;
}
for(i = 0; i < n; i++)
array[i] = aux[i];
free(count);
free(aux);
}
int main() {
int n, a, b, c;
int* array;
FILE *infile, *outfile;
infile = fopen("radixsort.in", "r");
outfile = fopen("radixsort.out", "w");
fscanf(infile, "%d", &n);
fscanf(infile, "%d %d %d", &a, &b, &c);
array = malloc(n * sizeof(int));
array[0] = b;
for (int i = 1; i < n; i++) {
array[i] = (1LL * a * array[i-1] + b) % c;
}
for (int i = 0; i < sizeof(int); i++)
radix_sort(array, n, i);
for (int i = 0; i < n; i=i+10)
fprintf(outfile, "%d ", array[i]);
free(array);
fclose(infile);
fclose(outfile);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}