Pagini recente » Cod sursa (job #1001100) | Cod sursa (job #2306788) | Cod sursa (job #2414038) | Cod sursa (job #3276604) | Cod sursa (job #2126393)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream f("radixsort.in" );
ofstream g("radixsort.out");
#define BITMASK ((1<<8)-1)
inline unsigned int byte(unsigned int x, unsigned int p) {
return (x>>(8*(p-1)))&BITMASK;
}
unsigned int output[10000003];
void countSort(unsigned int v[], unsigned int n, unsigned int exp)
{
unsigned int bucket[BITMASK+4] = {0};
// Store count of occurrences in count[]
for ( unsigned int i = 1; i <= n; i++ )
{
unsigned int grupa = byte(v[i], exp);
bucket[grupa]++;
}
// Change count[i] so that count[i] now contains actual
// position of this digit in output[]
for ( unsigned int i = 1; i <= BITMASK; i++ )
bucket[i] += bucket[i - 1];
// Build the output array
for ( unsigned int i = n; i >= 1; i-- )
{
unsigned int grupa = byte(v[i], exp);
output[--bucket[grupa]] = v[i];
}
// Copy the output vay to v[], so that v[] now
// contains sorted numbers according to current digit
for ( unsigned int i = 1; i <= n; i++ )
v[i] = output[i-1];
}
// The main function to that sorts arr[] of size n using
// Radix Sort
unsigned int N, cmax;
unsigned int v[10000003];
void GenerateArray() {
unsigned int A, B, C; f >> N >> A >> B >> C;
v[1] = B;
for ( unsigned int i=2; i<=N; i++ )
v[i] = (A * v[i-1] + B) % C;
}
void RadixSort(unsigned int arr[], unsigned int n) {
for (unsigned int cif = 1; cif <=4; cif++ )
countSort(arr, n, cif);
}
int main()
{
GenerateArray();
RadixSort(v, N);
for ( unsigned int i=0; i*10+1<=N; i++ )
g << v[i*10+1] << ' ';
}