Skip to content

Commit 2393b57

Browse files
author
Alex Klymenko
committed
fix: changing counting sort to keep it with standard implementation
1 parent 82aa523 commit 2393b57

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.thealgorithms.sorts;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* A standard implementation of the Counting Sort algorithm for integer arrays.
7+
* This implementation has a time complexity of O(n + k), where n is the number
8+
* of elements in the input array and k is the range of the input.
9+
* It works only with integer arrays.
10+
*
11+
* The space complexity is O(k), where k is the range of the input integers.
12+
*
13+
* Note: This implementation does not handle negative integers as it
14+
* calculates the range based on the minimum and maximum values of the array.
15+
*
16+
*/
17+
public final class CountingSort {
18+
private CountingSort() {
19+
}
20+
21+
/**
22+
* Sorts an array of integers using the Counting Sort algorithm.
23+
*
24+
* @param array the array to be sorted
25+
* @return the sorted array
26+
*/
27+
public static int[] sort(int[] array) {
28+
if (array.length == 0) {
29+
return array;
30+
}
31+
32+
int max = Arrays.stream(array).max().orElse(Integer.MIN_VALUE);
33+
int min = Arrays.stream(array).min().orElse(Integer.MAX_VALUE);
34+
int range = max - min + 1;
35+
36+
int[] count = new int[range];
37+
int[] output = new int[array.length];
38+
39+
for (int value : array) {
40+
count[value - min]++;
41+
}
42+
43+
for (int i = 1; i < count.length; i++) {
44+
count[i] += count[i - 1];
45+
}
46+
47+
for (int i = array.length - 1; i >= 0; i--) {
48+
output[count[array[i] - min] - 1] = array[i];
49+
count[array[i] - min]--;
50+
}
51+
52+
return output;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.MethodSource;
7+
import java.util.stream.Stream;
8+
9+
public class CountingSortTest {
10+
11+
record TestCase(int[] inputArray, int[] expectedArray) {
12+
}
13+
14+
static Stream<TestCase> provideTestCases() {
15+
return Stream.of(new TestCase(new int[] {}, new int[] {}), new TestCase(new int[] {4}, new int[] {4}), new TestCase(new int[] {6, 1, 99, 27, 15, 23, 36}, new int[] {1, 6, 15, 23, 27, 36, 99}), new TestCase(new int[] {6, 1, 27, 15, 23, 27, 36, 23}, new int[] {1, 6, 15, 23, 23, 27, 27, 36}), new TestCase(new int[] {5, 5, 5, 5, 5},
16+
new int[] {5, 5, 5, 5, 5}), new TestCase(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}), new TestCase(new int[] {5, 4, 3, 2, 1}, new int[] {1, 2, 3, 4, 5}), new TestCase(new int[] {3, -1, 4, 1, 5, -9}, new int[] {-9, -1, 1, 3, 4, 5}), new TestCase(new int[] {0, 0, 0, 0}, new int[] {0, 0, 0, 0}),
17+
new TestCase(new int[] {3, 3, -1, -1, 2, 2, 0, 0}, new int[] {-1, -1, 0, 0, 2, 2, 3, 3}), new TestCase(new int[] {-3, -2, -1, -5, -4}, new int[] {-5, -4, -3, -2, -1}), new TestCase(new int[] {1000, 500, 100, 50, 10, 5, 1}, new int[] {1, 5, 10, 50, 100, 500, 1000}));
18+
}
19+
20+
@ParameterizedTest
21+
@MethodSource("provideTestCases")
22+
public void testCountingSort(TestCase testCase) {
23+
int[] outputArray = CountingSort.sort(testCase.inputArray);
24+
assertArrayEquals(testCase.expectedArray, outputArray);
25+
}
26+
}

0 commit comments

Comments
 (0)