forked from algorithm019/algorithm019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSort.java
More file actions
37 lines (29 loc) · 968 Bytes
/
CountingSort.java
File metadata and controls
37 lines (29 loc) · 968 Bytes
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
import java.util.Arrays;
/**
* Description:计数排序
* <p>
* 计数排序不是基于比较的排序算法,其核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。
* 作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数
* <p>
* Date: 2020-12-26
* Time: 6:29 PM
*/
public class CountingSort {
public static void countingSort(int[] arr, int maxValue) {
int[] bucket = new int[maxValue + 1];
for (int i = 0; i < arr.length; i++) {
bucket[arr[i]]++;
}
int idx = 0;
for (int j = 0; j < bucket.length; j++) {
while (bucket[j] > 0) {
arr[idx++] = j;
bucket[j]--;
}
}
}
public static void main(String[] args) {
CountingSort.countingSort(SortConfig.arr, 21);
System.out.println(Arrays.toString(SortConfig.arr));
}
}