Pagini recente » Cod sursa (job #2645900) | Cod sursa (job #3225110) | Cod sursa (job #2655483) | Cod sursa (job #121725) | Cod sursa (job #1328924)
#include <fstream>
#include <vector>
#include <list>
#include <cstring>
#define get_byte(x) ((x>>(byte * 8))&RADIX)
using namespace std;
ifstream fin ("radixsort.in");
ofstream fout ("radixsort.out");
const int BASE = 10;
const int MAX_N = 10000000 + 1;
int numbers[MAX_N];
const int RADIX = 0xFF;
const int RADIX_SIZE = 8;
const int TOTAL_BYTES = sizeof(numbers[0]);
int n;
void countsort(int A[], int B[], int byte) {
int counter[1<<RADIX_SIZE];
int index[1<<RADIX_SIZE];
memset(counter, 0, sizeof(counter));
for(int i = 0; i < n; i ++)
++counter[get_byte(A[i])];
index[0] = 0;
for(int i = 1; i < 1<<RADIX_SIZE; i ++)
index[i] = index[i-1] + counter[i-1];
for(int i = 0; i < n; i ++)
B[index[get_byte(A[i])]++] = A[i];
}
void radixsort() {
int *temp = new int[n]; // avem nevoie de un array ca spatiu temporar
for (int i = 0; i < TOTAL_BYTES; i ++) {
if(i % 2 == 0)
countsort(numbers, temp, i);// sortez dupa byte-ul i
else
countsort(temp, numbers, i);
}
}
inline void read()
{
int a,b,c;
fin>>n>>a>>b>>c;
numbers[0] = b % c;
for(int i = 1; i < n; i ++)
numbers[i] = (1LL * a * numbers[i-1] % c + b) % c;
fin.close();
}
inline void write(ofstream& f)
{
for(int i = 0; i < n; i +=10)
f << numbers[i]<< ' ';
f<<endl;
f.close();
}
int main()
{
read();
radixsort();
write(fout);
return 0;
}