-
Notifications
You must be signed in to change notification settings - Fork 14
/
RadixSort.java
84 lines (69 loc) · 2.13 KB
/
RadixSort.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package leetcode.sort.algorithms;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
/**
* 基数排序
*
* @author FangYuan
* @since 2023-11-17 15:40:29
*/
public class RadixSort {
public static void main(String[] args) {
new RadixSort().sort(new int[]{3, 100, 11, 4, 33});
}
private void sort(int[] nums) {
if (nums.length <= 1) {
return;
}
// 1. 整数的每位取值范围为 0-9,因此需要创建10个桶
Queue<Integer>[] buckets = createBuckets();
// 2. 获取基数排序的执行轮次
int radixRounds = getRadixRounds(nums);
// 3. 根据执行轮次处理各个"位",eg: 第一轮处理个位...
for (int round = 1; round <= radixRounds; round++) {
for (int num : nums) {
// 获取所在桶的索引
int bucketIndex = getBucketIndex(num, round);
// 进桶
buckets[bucketIndex].offer(num);
}
// 出桶赋值,当前结果为根据当前位排序的结果
int numsIndex = 0;
for (Queue<Integer> bucket : buckets) {
while (!bucket.isEmpty()) {
nums[numsIndex++] = bucket.poll();
}
}
}
}
/**
* 创建大小为10的数组作为桶,每个桶都是一个队列
*/
@SuppressWarnings("unchecked")
private Queue<Integer>[] createBuckets() {
Queue<Integer>[] buckets = new Queue[10];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = new LinkedList<>();
}
return buckets;
}
/**
* 获取基数排序的执行轮次
*/
private int getRadixRounds(int[] nums) {
return String.valueOf(Arrays.stream(nums).max().getAsInt()).length();
}
/**
* 获取该数所在桶的索引
*/
private int getBucketIndex(int num, int round) {
int bucketIndex = 0;
while (round != 0) {
bucketIndex = num % 10;
num /= 10;
round--;
}
return bucketIndex;
}
}