10000 Create radix_sort.cpp · rinrin528/basic-algo-lecture@628476e · GitHub
[go: up one dir, main page]

Skip to content

Commit 628476e

Browse files
Create radix_sort.cpp
1 parent f02037d commit 628476e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

0x0F/radix_sort.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int n = 15;
5+
int arr[1000001] = {12, 421, 46, 674, 103, 502, 7, 100, 21, 545, 722, 227, 62, 91, 240};
6+
7+
// 1, 10, 100의 자리에 대해서 진행, 수가 최대 3자리수가 아닐 경우 d를 조절해야 함
8+
int d = 3;
9+
// p10[i] = 10의 i승
10+
int p10[3];
11+
12+
// x의 10^a 자리의 값을 반환하는 함수
13+
int digitNum(int x, int a){
14+
return (x / p10[a]) % 10;
15+
}
16+
17+
vector<int> l[10];
18+
int main(){
19+
ios::sync_with_stdio(0);
20+
cin.tie(0);
21+
22+
p10[0] = 1;
23+
for(int i = 1; i < d; i++) p10[i] = p10[i-1] * 10;
24+
25+
for(int i = 0; i < d; i++){
26+
for(int j = 0; j < 10; j++) l[j].clear();
27+
// 각 수를 list에 대입
28+
for(int j = 0; j < n; j++)
29+
l[digitNum(arr[j], i)].push_back(arr[j]);
30+
// 0번 리스트부터 차례로 다시 arr에 넣어 재배열
31+
int aidx = 0;
32+
for(int j = 0; j < 10; j++){
33+
for(auto x : l[j]) arr[aidx++] = x;
34+
}
35+
}
36+
for(int i = 0; i < n; i++) cout << arr[i] << ' ';
37+
}

0 commit comments

Comments
 (0)
0