Skip to content

Commit 9403214

Browse files
alxkmAlex Klymenkovil02
authored
refactor: cleanup RadixSort (#5280)
* refactor: refactoring RadixSort, adding test, update DIRECTORY.md * checkstyle: fix formatting for test * refactor: adding possibility to sort negative numbers. Improve tests. Improving code readability * checkstyle: fix formatting * refactor: resolve conflicts with master branch * refactor: remove negative integers support * checkstyle: fix formatting * checkstyle: fix formatting, revert test * refactor: adding return array to countDigits and buildOutput method, adding more specific description to javadocs --------- Co-authored-by: Alex Klymenko <[email protected]> Co-authored-by: Piotr Idzik <[email protected]>
1 parent f1e2606 commit 9403214

File tree

3 files changed

+103
-36
lines changed

3 files changed

+103
-36
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@
882882
* [OddEvenSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java)
883883
* [PancakeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PancakeSortTest.java)
884884
* [QuickSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/QuickSortTest.java)
885+
* [RadixSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/RadixSortTest.java)
885886
* [SelectionSortRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java)
886887
* [SelectionSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortTest.java)
887888
* [ShellSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/ShellSortTest.java)

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

+72-36
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,98 @@
11
package com.thealgorithms.sorts;
22

3+
import com.thealgorithms.maths.NumberOfDigits;
34
import java.util.Arrays;
45

5-
final class RadixSort {
6+
/**
7+
* This class provides an implementation of the radix sort algorithm.
8+
* It sorts an array of nonnegative integers in increasing order.
9+
*/
10+
public final class RadixSort {
11+
private static final int BASE = 10;
12+
613
private RadixSort() {
714
}
815

9-
private static int getMax(int[] arr, int n) {
10-
int mx = arr[0];
11-
for (int i = 1; i < n; i++) {
12-
if (arr[i] > mx) {
13-
mx = arr[i];
14-
}
16+
/**
17+
* Sorts an array of nonnegative integers using the radix sort algorithm.
18+
*
19+
* @param array the array to be sorted
20+
* @return the sorted array
21+
* @throws IllegalArgumentException if any negative integers are found
22+
*/
23+
public static int[] sort(int[] array) {
24+
if (array.length == 0) {
25+
return array;
1526
}
16-
return mx;
17-
}
1827

19-
private static void countSort(int[] arr, int n, int exp) {
20-
int[] output = new int[n];
21-
int i;
22-
int[] count = new int[10];
23-
Arrays.fill(count, 0);
28+
checkForNegativeInput(array);
29+
radixSort(array);
30+
return array;
31+
}
2432

25-
for (i = 0; i < n; i++) {
26-
count[(arr[i] / exp) % 10]++;
33+
/**
34+
* Checks if the array contains any negative integers.
35+
*
36+
* @param array the array to be checked
37+
* @throws IllegalArgumentException if any negative integers are found
38+
*/
39+
private static void checkForNegativeInput(int[] array) {
40+
for (int number : array) {
41+
if (number < 0) {
42+
throw new IllegalArgumentException("Array contains non-positive integers.");
43+
}
2744
}
45+
}
2846

29-
for (i = 1; i < 10; i++) {
30-
count[i] += count[i - 1];
47+
private static void radixSort(int[] array) {
48+
final int max = Arrays.stream(array).max().getAsInt();
49+
for (int i = 0, exp = 1; i < NumberOfDigits.numberOfDigits(max); i++, exp *= BASE) {
50+
countingSortByDigit(array, exp);
3151
}
52+
}
3253

33-
for (i = n - 1; i >= 0; i--) {
34-
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
35-
count[(arr[i] / exp) % 10]--;
36-
}
54+
/**
55+
* A utility method to perform counting sort of array[] according to the digit represented by exp.
56+
*
57+
* @param array the array to be sorted
58+
* @param exp the exponent representing the current digit position
59+
*/
60+
private static void countingSortByDigit(int[] array, int exp) {
61+
int[] count = countDigits(array, exp);
62+
accumulateCounts(count);
63+
int[] output = buildOutput(array, exp, count);
64+
copyOutput(array, output);
65+
}
3766

38-
System.arraycopy(output, 0, arr, 0, n);
67+
private static int[] countDigits(int[] array, int exp) {
68+
int[] count = new int[BASE];
69+
for (int i = 0; i < array.length; i++) {
70+
count[getDigit(array[i], exp)]++;
71+
}
72+
return count;
3973
}
4074

41-
private static void radixsort(int[] arr, int n) {
42-
int m = getMax(arr, n);
75+
private static int getDigit(int number, int position) {
76+
return (number / position) % BASE;
77+
}
4378

44-
for (int exp = 1; m / exp > 0; exp *= 10) {
45-
countSort(arr, n, exp);
79+
private static void accumulateCounts(int[] count) {
80+
for (int i = 1; i < BASE; i++) {
81+
count[i] += count[i - 1];
4682
}
4783
}
4884

49-
static void print(int[] arr, int n) {
50-
for (int i = 0; i < n; i++) {
51-
System.out.print(arr[i] + " ");
85+
private static int[] buildOutput(int[] array, int exp, int[] count) {
86+
int[] output = new int[array.length];
87+
for (int i = array.length - 1; i >= 0; i--) {
88+
int digit = getDigit(array[i], exp);
89+
output[count[digit] - 1] = array[i];
90+
count[digit]--;
5291
}
92+
return output;
5393
}
5494

55-
public static void main(String[] args) {
56-
int[] arr = {170, 45, 75, 90, 802, 24, 2, 66};
57-
int n = arr.length;
58-
radixsort(arr, n);
59-
print(arr, n);
95+
private static void copyOutput(int[] array, int[] output) {
96+
System.arraycopy(output, 0, array, 0, array.length);
6097
}
6198
}
62-
// Written by James Mc Dermott(theycallmemac)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
public class RadixSortTest {
13+
@ParameterizedTest
14+
@MethodSource("provideTestCases")
15+
public void test(int[] inputArray, int[] expectedArray) {
16+
assertArrayEquals(RadixSort.sort(inputArray), expectedArray);
17+
}
18+
19+
private static Stream<Arguments> provideTestCases() {
20+
return Stream.of(Arguments.of(new int[] {170, 45, 75, 90, 802, 24, 2, 66}, new int[] {2, 24, 45, 66, 75, 90, 170, 802}), Arguments.of(new int[] {3, 3, 3, 3}, new int[] {3, 3, 3, 3}), Arguments.of(new int[] {9, 4, 6, 8, 14, 3}, new int[] {3, 4, 6, 8, 9, 14}),
21+
Arguments.of(new int[] {10, 90, 49, 2, 1, 5, 23}, new int[] {1, 2, 5, 10, 23, 49, 90}), Arguments.of(new int[] {1, 3, 4, 2, 7, 8}, new int[] {1, 2, 3, 4, 7, 8}), Arguments.of(new int[] {}, new int[] {}), Arguments.of(new int[] {1}, new int[] {1}),
22+
Arguments.of(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}, new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}), Arguments.of(new int[] {9, 8, 7, 6, 5, 4, 3, 2, 1}, new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}),
23+
Arguments.of(new int[] {1000000000, 999999999, 888888888, 777777777}, new int[] {777777777, 888888888, 999999999, 1000000000}), Arguments.of(new int[] {123, 9, 54321, 123456789, 0}, new int[] {0, 9, 123, 54321, 123456789}));
24+
}
25+
26+
@Test
27+
public void testWithNegativeNumbers() {
28+
assertThrows(IllegalArgumentException.class, () -> RadixSort.sort(new int[] {3, 1, 4, 1, 5, -9}));
29+
}
30+
}

0 commit comments

Comments
 (0)