File tree Expand file tree Collapse file tree 1 file changed +69
-0
lines changed Expand file tree Collapse file tree 1 file changed +69
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < cstdio>
2
+ #include < climits>
3
+
4
+ #define INF INT_MAX
5
+
6
+ #define ELEMENT_COUNT 100000
7
+ #define ELEMENT_RANGE (1 << 17 )
8
+ #define GROUP_RANGE (1 << 2 )
9
+
10
+ using namespace std ;
11
+
12
+ int n, d[ELEMENT_COUNT];
13
+
14
+ struct node
15
+ {
16
+ int v, next;
17
+ }A[ELEMENT_COUNT + 1 ];
18
+ int head[ELEMENT_RANGE / GROUP_RANGE], cnt[ELEMENT_RANGE / GROUP_RANGE], vc = 1 ;
19
+
20
+ void insert (int v)
21
+ {
22
+ int group = v / GROUP_RANGE;
23
+ A[vc].next = head[group];
24
+ head[group] = vc;
25
+ A[vc].v = v;
26
+ cnt[group]++;
27
+ vc++;
28
+ }
29
+
30
+ void bucket_sort ()
31
+ {
32
+ for (int i = 0 ; i < n; i++)
33
+ {
34
+ insert (d[i]);
35
+ }
36
+ int ptr = 0 ;
37
+ for (int i = 0 ; i < ELEMENT_RANGE / GROUP_RANGE; i++)
38
+ {
39
+ for (int j = 0 ; j < cnt[i]; j++)
40
+ {
41
+ int minv = INF, ord;
42
+ for (int cur = head[i]; cur != 0 ; cur = A[cur].next )
43
+ {
44
+ if (A[cur].v < minv)
45
+ {
46
+ minv = A[cur].v ;
47
+ ord = cur;
48
+ }
49
+ }
50
+ d[ptr++] = minv;
51
+ A[ord].v = INF;
52
+ }
53
+ }
54
+ }
55
+
56
+ int main ()
57
+ {
58
+ scanf (" %d" , &n);
59
+ for (int i = 0 ; i < n; i++)
60
+ {
61
+ scanf (" %d" , &d[i]);
62
+ }
63
+ bucket_sort ();
64
+ for (int i = 0 ; i < n; i++)
65
+ {
66
+ printf (" %d " , d[i]);
67
+ }
68
+ return 0 ;
69
+ }
You can’t perform that action at this time.
0 commit comments