-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
44 lines (38 loc) · 959 Bytes
/
main2.cpp
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
/*
从个位开始排序。
*/
#include "mycommon.h"
using namespace std;
const static int BASE = 2;
int getdigit(int num, int e) { return (num / e) % BASE; } // 取出数字num的第e位数字
void s(vector<int> &nums, int e)
{
vector<int> count(BASE, 0);
for (int i = 0; i < nums.size(); i++) count[getdigit(nums[i], e)]++;
for (int i = 1; i < count.size(); i++) count[i] += count[i - 1];
vector<int> result(nums.size(), 0);
for (int i = nums.size() - 1; i >= 0; i--)
{
int j = getdigit(nums[i], e);
result[count[j] - 1] = nums[i];
count[j]--;
}
nums = result;
}
void radixsort(vector<int> &nums)
{
int max = *max_element(nums.begin(), nums.end());
for (int e = 1; max / e > 0; e *= BASE) s(nums, e);
}
void printvector(vector<int> &nums)
{
for (auto i : nums) printf("%d ", i);
}
int main()
{
// BASE=2;
vector<int> nums = {11, 10, 111, 1111, 1010, 1001};
radixsort(nums);
printvector(nums);
return 0;
}