-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSort
More file actions
52 lines (40 loc) · 1.26 KB
/
CountingSort
File metadata and controls
52 lines (40 loc) · 1.26 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
51
52
package main.sorting;
import java.util.Arrays;
/**
* 非交换的排序的优点是效率高;
* 缺点是对于入参需要是整型,同时入参的规模一旦变大,需要较多的空间
*
*
*/
public class CountingSort {
public static final Integer SIZE = 15;
public static final Integer RANGE = 100;
public static void main(String[] args) {
final RandomSeed seed = new RandomSeed(SIZE);
Integer[] input = seed.getRandomSeedForSorting(RANGE);
for (Integer i : input) {
System.out.print(i + ", ");
}
System.out.println("After Sort :");
Integer[] result = countSort(input, RANGE);
for (Integer integer : result) {
System.out.print(integer + ", ");
}
}
public static Integer[] countSort(Integer[] data, Integer range) {
int[] counter = new int[range];
for (Integer item : data) {
counter[item] += 1;
}
Integer[] result = new Integer[data.length];
int pos = 0;
for (int k = 0; k < counter.length; k++) {
if(counter[k] > 0) {
for(int i = 0; i < counter[k]; i++ ){
result[pos++] = k;
}
}
}
return result;
}
}