-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.cpp
More file actions
55 lines (42 loc) · 1.01 KB
/
RadixSort.cpp
File metadata and controls
55 lines (42 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
using namespace std;
ifstream fin("radixsort.in");
ofstream fout("radixsort.out");
#define get_byte(x) ((x >> (byte * 16)) & RADIX)
const int kN = 1e7;
const int RADIX = 65535;
const int RADIX_SIZE = 16;
int n, v[kN], temp[kN];
void countSort(int A[], int B[], int byte) {
int counter[1 << RADIX_SIZE], index[1 << RADIX_SIZE];
memset(counter, 0, sizeof(counter));
for (int i = 0; i < n; ++i) {
counter[get_byte(A[i])] += 1;
}
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];
}
}
int main () {
int a, b, c;
fin >> n >> a >> b >> c;
v[0] = b % c;
for (int i = 1; i < n; ++i) {
v[i] = (1LL * a * v[i - 1] + b) % c;
}
countSort(v, temp, 0);
countSort(temp, v, 1);
for (int i = 0; i < n; i += 10) {
fout << v[i] << ' ';
}
fin.close();
fout.close();
return 0;
}