aboutsummaryrefslogtreecommitdiff
path: root/rcc-legacy/tests/sysy_lvX/radix_sort.sy
blob: d8e1cee97f23ed3076b6f6886b6714f75712121f (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// 你可以用这个生成数据
// import numpy as np
//
// print(2000000)
// for x in np.random.choice(2000000, 2000000):
//     print(x, end=' ')

int n;
int aux[2000000];


void radix_sort(int a[], int n) {
  const int U = 65536;
  int i;
  // 不行, segment fault
  // int cnt[65536] = {};
  // 可以
  int cnt[65536];

  i = 0;
  while (i < U) {
    cnt[i] = 0;
    i = i + 1;
  }

  i = 0;
  while (i < n) {
    cnt[a[i] % U] = cnt[a[i] % U] + 1;
    i = i + 1;
  }
  i = 1;
  while (i < U) {
    cnt[i] = cnt[i] + cnt[i - 1];
    i = i + 1;
  }
  i = n - 1;
  while (i >= 0) {
    cnt[a[i] % U] = cnt[a[i] % U] - 1;
    aux[cnt[a[i] % U]] = a[i];
    i = i - 1;
  }

  i = 0;
  while (i < U) {
    cnt[i] = 0;
    i = i + 1;
  }

  i = 0;
  while (i < n) {
    cnt[aux[i] / U % U] = cnt[aux[i] / U % U] + 1;
    i = i + 1;
  }
  i = 1;
  while (i < U) {
    cnt[i] = cnt[i] + cnt[i - 1];
    i = i + 1;
  }
  i = n - 1;
  while (i >= 0) {
    cnt[aux[i] / U % U] = cnt[aux[i] / U % U] - 1;
    a[cnt[aux[i] / U % U]] = aux[i];
    i = i - 1;
  }
}

int main(){
  starttime();
  int a[2000000];
  n = getarray(a);
  radix_sort(a, n);
  putarray(n, a);
  stoptime();
  return 0;
}