Skip to content

Commit 0b588cd

Browse files
authored
Added a new program named "CountingSortApp"
Created with Java with the help of Arrays
1 parent d4f6b3d commit 0b588cd

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

CountingSortApp.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Arrays;
2+
3+
class CountingSortApp{
4+
5+
public static void main(String[] args) {
6+
7+
System.out.println("Counting sort in Java");
8+
int[] input = { 60, 40, 30, 20, 10, 40, 30, 60, 60, 20, 40, 30, 40 };
9+
int k = 60;
10+
11+
System.out.println("integer array before sorting");
12+
System.out.println(Arrays.toString(input));
13+
14+
countingSort(input, k);
15+
16+
System.out.println("integer array after sorting using counting sort
17+
algorithm");
18+
System.out.println(Arrays.toString(input));
19+
20+
}
21+
22+
public static void countingSort(int[] input, int k) {
23+
int counter[] = new int[k + 1];
24+
25+
for (int i : input) {
26+
counter[i]++;
27+
}
28+
29+
int ndx = 0;
30+
for (int i = 0; i < counter.length; i++) {
31+
while (0 < counter[i]) {
32+
input[ndx++] = i;
33+
counter[i]--;
34+
}
35+
}
36+
}
37+
38+
}

0 commit comments

Comments
 (0)