Skip to content

Commit d3a6a7e

Browse files
alimatemaibin
authored andcommitted
Sample Codes for Counting Sort in Java (eugenp#7684)
* Added the code samples. * Updated the Readme.
1 parent e8f8343 commit d3a6a7e

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

algorithms-sorting/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
- [Insertion Sort in Java](https://www.baeldung.com/java-insertion-sort)
77
- [Heap Sort in Java](https://www.baeldung.com/java-heap-sort)
88
- [Shell Sort in Java](https://www.baeldung.com/java-shell-sort)
9+
- [Counting Sort in Java](https://www.baeldung.com/java-counting-sort)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.algorithms.counting;
2+
3+
import java.util.Arrays;
4+
import java.util.stream.IntStream;
5+
6+
public class CountingSort {
7+
8+
public static int[] sort(int[] input, int k) {
9+
verifyPreconditions(input, k);
10+
if (input.length == 0) return input;
11+
12+
int[] c = countElements(input, k);
13+
int[] sorted = new int[input.length];
14+
for (int i = input.length - 1; i >= 0; i--) {
15+
int current = input[i];
16+
sorted[c[current] - 1] = current;
17+
c[current] -= 1;
18+
}
19+
20+
return sorted;
21+
}
22+
23+
static int[] countElements(int[] input, int k) {
24+
int[] c = new int[k + 1];
25+
Arrays.fill(c, 0);
26+
for (int i : input) {
27+
c[i] += 1;
28+
}
29+
30+
for (int i = 1; i < c.length; i++) {
31+
c[i] += c[i - 1];
32+
}
33+
return c;
34+
}
35+
36+
private static void verifyPreconditions(int[] input, int k) {
37+
if (input == null) {
38+
throw new IllegalArgumentException("Input is required");
39+
}
40+
41+
int min = IntStream.of(input).min().getAsInt();
42+
int max = IntStream.of(input).max().getAsInt();
43+
44+
if (min < 0 || max > k) {
45+
throw new IllegalArgumentException("The input numbers should be between zero and " + k);
46+
}
47+
}
48+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.algorithms.counting;
2+
3+
import java.util.Arrays;
4+
5+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
class CountingSortUnitTest {
10+
11+
@Test
12+
void countElements_GivenAnArray_ShouldCalculateTheFrequencyArrayAsExpected() {
13+
int k = 5;
14+
int[] input = { 4, 3, 2, 5, 4, 3, 5, 1, 0, 2, 5 };
15+
16+
int[] c = CountingSort.countElements(input, k);
17+
int[] expected = { 1, 2, 4, 6, 8, 11 };
18+
assertArrayEquals(expected, c);
19+
}
20+
21+
@Test
22+
void sort_GivenAnArray_ShouldSortTheInputAsExpected() {
23+
int k = 5;
24+
int[] input = { 4, 3, 2, 5, 4, 3, 5, 1, 0, 2, 5 };
25+
26+
int[] sorted = CountingSort.sort(input, k);
27+
28+
// Our sorting algorithm and Java's should return the same result
29+
Arrays.sort(input);
30+
assertArrayEquals(input, sorted);
31+
}
32+
}

0 commit comments

Comments
 (0)