File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
yoonexample/src/main/java/sort Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ package sort ;
2
+
3
+ import queue .LinkedListQueue ;
4
+ import queue .Queue ;
5
+
6
+ public class RadixSort {
7
+
8
+ private static final int BUCKET_NUM = 10 ;
9
+
10
+ public void sort (int [] array , int maxLength ) {
11
+ int divFac = 1 ;
12
+ Queue <Integer >[] buckets = new Queue [BUCKET_NUM ];
13
+
14
+ // 버킷 배열 초기화
15
+ for (int i = 0 ; i < BUCKET_NUM ; i ++) {
16
+ buckets [i ] = new LinkedListQueue <>();
17
+ }
18
+
19
+ // 가장 긴 데이터의 길이만큼 반복
20
+ for (int i = 0 ; i < maxLength ; i ++) {
21
+ // 정렬 대상 수의 개수만큼 반복
22
+ for (int num : array ) {
23
+ // N번째 자리의 숫자 추출
24
+ int radix = (num / divFac ) % 10 ;
25
+
26
+ // 추출한 숫자에 위치하도록 데이터 저장
27
+ buckets [radix ].enqueue (num );
28
+ }
29
+
30
+ // 버킷 수 만큼 반복
31
+ for (int j = 0 ; j < BUCKET_NUM ; j ++) {
32
+ // 버킷에 저장되어 있는 것을 순서대로 꺼내서 다시 array에 저장
33
+ int arrayIndex = 0 ;
34
+ while (!buckets [j ].isEmpty ()) {
35
+ array [arrayIndex ++] = buckets [j ].dequeue ();
36
+ }
37
+ }
38
+
39
+ // N번째 자리 숫자를 추출할 수 있도록 변경
40
+ divFac *= 10 ;
41
+ }
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments