Skip to content

Commit 5613eae

Browse files
author
Alex Klymenko
committed
refactor: restructure sorting, update docs and tests
1 parent 1dcc268 commit 5613eae

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/main/java/com/thealgorithms/sorts/CountingSort.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* The space complexity is O(k), where k is the range of the input integers.
1212
*
13-
* Note: This implementation does not handle negative integers as it
13+
* Note: This implementation handle negative integers as it
1414
* calculates the range based on the minimum and maximum values of the array.
1515
*
1616
*/
@@ -28,27 +28,33 @@ public static int[] sort(int[] array) {
2828
if (array.length == 0) {
2929
return array;
3030
}
31+
final var stats = Arrays.stream(array).summaryStatistics();
32+
final int min = stats.getMin();
33+
int[] count = computeHistogram(array, min, stats.getMax() - min + 1);
34+
toCumulative(count);
35+
return reconstructSorted(count, min, array);
36+
}
3137

32-
final int max = Arrays.stream(array).max().orElse(Integer.MIN_VALUE);
33-
final int min = Arrays.stream(array).min().orElse(Integer.MAX_VALUE);
34-
final 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]++;
38+
private static int[] computeHistogram(final int[] array, final int shift, final int spread) {
39+
int[] res = new int[spread];
40+
for (final var value : array) {
41+
res[value - shift]++;
4142
}
43+
return res;
44+
}
4245

46+
private static void toCumulative(int[] count) {
4347
for (int i = 1; i < count.length; i++) {
4448
count[i] += count[i - 1];
4549
}
50+
}
4651

52+
private static int[] reconstructSorted(final int[] cumulativeCount, final int shift, final int[] array) {
53+
int[] res = new int[array.length];
4754
for (int i = array.length - 1; i >= 0; i--) {
48-
output[count[array[i] - min] - 1] = array[i];
49-
count[array[i] - min]--;
55+
res[cumulativeCount[array[i] - shift] - 1] = array[i];
56+
cumulativeCount[array[i] - shift]--;
5057
}
51-
52-
return output;
58+
return res;
5359
}
5460
}

src/test/java/com/thealgorithms/sorts/CountingSortTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static Stream<TestCase> provideTestCases() {
1515
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}),
1616
new TestCase(new int[] {5, 5, 5, 5, 5}, 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}),
1717
new TestCase(new int[] {0, 0, 0, 0}, new int[] {0, 0, 0, 0}), 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}),
18-
new TestCase(new int[] {1000, 500, 100, 50, 10, 5, 1}, new int[] {1, 5, 10, 50, 100, 500, 1000}));
18+
new TestCase(new int[] {1000, 500, 100, 50, 10, 5, 1}, new int[] {1, 5, 10, 50, 100, 500, 1000}), new TestCase(new int[] {4, -5, 10, 0}, new int[] {-5, 0, 4, 10}));
1919
}
2020

2121
@ParameterizedTest

0 commit comments

Comments
 (0)