-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.java
More file actions
50 lines (42 loc) · 1.46 KB
/
Copy pathRadixSort.java
File metadata and controls
50 lines (42 loc) · 1.46 KB
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
package cx.Sort;
import cx.Linear.MyQueue;
import java.util.Arrays;
/**
* 基数排序,分配式排序,每次按照数字的个位、十位...将元素分配至某些桶中
*/
public class RadixSort {
public static void main(String[] args) {
int[] arr = new int[]{23, 6, 189, 45, 9, 287, 56, 1, 798, 34, 65, 652, 5};
radixSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void radixSort(int[] arr) {
//找到数组中最大数,确定排序次数
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
//小tip:将数字转换成字符串从而计算其位数
int maxLength = (max + "").length();
MyQueue[] temp = new MyQueue[10];
for (int i = 0; i < temp.length; i++) temp[i] = new MyQueue();
//不断除10取余
int n = 1;
for (int i = 0; i < maxLength; i++) {
//遍历数组,将每一个数放入指定的队列
for (int j = 0; j < arr.length; j++) {
int nums = arr[j] / n % 10;
temp[nums].add(arr[j]);
}
//依次取数
int index = 0;
for (int k = 0; k < temp.length; k++) {
while (!temp[k].isEmpty()) {
arr[index++] = temp[k].poll();
}
}
//取下一位
n *= 10;
}
}
}