https://www.runoob.com/w3cnote/radix-sort.html
1 // 基数排序.cpp: 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include <iostream>
6 #include <vector>
7 #include <math.h>
8
9 using namespace std;
10
11 int RADIX(vector<int> &A,int d)//出于简单考虑,我们假设A中所有的数都是d位
12 {
13 vector<vector<int>> bucket(10);//用二维数组也行
14 int temp;
15 for (int i = 0; i < d; i++)
16 {
17 for (int j = 0; j < A.size(); j++)
18 {
19 temp = (int)(A[j] / pow(10, i)) % 10;
20 bucket[temp].push_back(A[j]);
21 }
22 int ptr = 0;
23 for (int i = 0; i < 10; i++)
24 {
25 for (int j = 0; j < bucket[i].size(); j++)
26 {
27 A[ptr++] = bucket[i][j];
28 }
29 bucket[i].erase(bucket[i].begin(), bucket[i].end());
30 }
31 }
32 return 0;
33 }
34
35 int main()
36 {
37 vector<int> A = {329,457,657,839,436,720,355};
38 for (auto c : A)
39 cout << c << ends;
40 cout << endl;
41
42 RADIX(A,3);
43 for (auto c : A)
44 cout << c << ends;
45 cout << endl;
46 return 0;
47 }