forked from VaibhavD74/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSort.java
More file actions
30 lines (26 loc) · 913 Bytes
/
CountingSort.java
File metadata and controls
30 lines (26 loc) · 913 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
import java.util.Arrays;
public class CountiSorter {
public static void main(String[] args) {
System.out.println("Counting sort in Java");
int[] input = { 60, 40, 30, 20, 10, 40, 30, 60, 60, 20, 40, 30, 40 };
int k = 60;
System.out.println("integer array before sorting");
System.out.println(Arrays.toString(input));
countingSort(input, k);
System.out.println("integer array after sorting using counting sort algorithm");
System.out.println(Arrays.toString(input));
}
public static void countingSort(int[] input, int k) {
int counter[] = new int[k + 1];
for (int i : input) {
counter[i]++;
}
int ndx = 0;
for (int i = 0; i < counter.length; i++) {
while (0 < counter[i]) {
input[ndx++] = i;
counter[i]--;
}
}
}
}