forked from yangyiRunning/DataStructureAlgorithmsJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSort.java
More file actions
48 lines (45 loc) · 1.35 KB
/
CountSort.java
File metadata and controls
48 lines (45 loc) · 1.35 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
package ds;
/**
* 计数排序
* <p>
* 总共分4步:
* <p>
* 1. 先遍历待排序数组找出数组中的最大值
* 2. 以此最大值为依据开辟一个新的数组空间
* 3. 遍历待排序数组,其中的每一个值放在新数组的相同值的下标当中
* 4. 遍历新数组,输出结果
*
* @author yangyi 2019年02月12日11:14:44
*/
public class CountSort {
public int[] countSort(int[] array) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
int[] countArray = new int[max + 1];
for (int i = 0; i < array.length; i++) {
countArray[array[i]]++;
}
int index = 0;
int[] sortArray = new int[array.length];
for (int i = 0; i < countArray.length; i++) {
for (int j = 0; j < countArray[i]; j++) {
sortArray[index] = i;
index++;
}
}
return sortArray;
}
public static void main(String[] args) {
int[] a = {4, 4, 6, 5, 3, 2, 8, 1, 7, 5, 6, 0, 10};
CountSort countSort = new CountSort();
int[] res = countSort.countSort(a);
System.out.println("排序后输出一下:");
for (int re : res) {
System.out.println(re);
}
}
}