-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSort.java
More file actions
37 lines (30 loc) · 841 Bytes
/
Copy pathCountSort.java
File metadata and controls
37 lines (30 loc) · 841 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
// search and sort. Maximum known and should be non-negative?
class CountSort {
public static void main(String[] args) {
int[] array = {4, 2, 9, 6, 23, 12, 34, 0, 34, 100};
countSort(array, 101);
printArray(array);
}
public static void countSort(int[] array, int count) {
int[] countArray = new int[count];
for (int i = 0; i < array.length; i++) {
int index = array[i];
countArray[index]++;
}
int k = 0;
for (int j = 0; j < count; j++) {
int numberCount = countArray[j];
while (numberCount > 0) {
array[k] = j;
numberCount--;
k++;
}
}
}
private static void printArray(int[] input) {
for (int i = 0; i < input.length - 1; i++) {
System.out.print(input[i] + ", ");
}
System.out.println(input[input.length-1]);
}
}