-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCountingSort.java
38 lines (32 loc) · 926 Bytes
/
CountingSort.java
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
public class CountingSort {
public static void countingSort(int[] array) {
int n = array.length;
int largeNum = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
largeNum = Math.max(largeNum, array[i]);
}
int[] count = new int[largeNum+1];
for (int i = 0; i<n; i++) {
count[array[i]]++;
}
//Sorting
int j = 0;
for (int i = 0; i<count.length; i++) {
while (count[i] > 0) {
array[j] = i;
j++;
count[i]--;
}
}
}
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static void main(String[] sad0xer) {
int[] array = {1, 4, 1, 3, 2, 4, 3, 7};
countingSort(array);
printArray(array);
}
}