-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort
More file actions
60 lines (53 loc) · 926 Bytes
/
RadixSort
File metadata and controls
60 lines (53 loc) · 926 Bytes
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
#include <stdio.h>
union Int32
{
int x;
unsigned char bytes[4];
};
int key(union Int32 *a, int ind, int j)
{
if ((a[j].x) >= 0)
{
return 256 + a[j].bytes[ind];
}
else
{
return a[j].bytes[ind];
}
}
void dist(union Int32 *a, int n, int ind)
{
int count[256 * 2];
int i, k;
for (i = 0; i < 256 * 2; i++) count[i] = 0;
for (int j = 0; j < n; j++)
{
k = key(a, ind, j);
count[k]++;
}
for (i = 1; i < 256 * 2; i++) count[i] += count[i - 1];
union Int32 ans[n];
for (int j = n - 1; j >= 0; j--)
{
k = key(a, ind, j);
i = count[k] - 1;
count[k] = i;
ans[i] = a[j];
}
for (int i = 0; i < n; i++) a[i] = ans[i];
}
void radix(union Int32 *a, int n)
{
for (int i = 0; i < 4; i++)
dist(a, n, i);
}
int main()
{
int n, i;
scanf("%d", &n);
union Int32 nums[n];
for (i = 0; i < n; i++) scanf("%d", &nums[i].x);
radix(nums, n);
for (i = 0; i < n; i++) printf("%d ", nums[i].x);
return 0;
}