Skip to content

Commit b218afa

Browse files
author
Alex Klymenko
committed
refactor: refactoring RadixSort, adding test, update DIRECTORY.md
1 parent 57f6580 commit b218afa

File tree

3 files changed

+62
-40
lines changed

3 files changed

+62
-40
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -873,9 +873,11 @@
873873
* [IntrospectiveSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/IntrospectiveSortTest.java)
874874
* [MergeSortRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/MergeSortRecursiveTest.java)
875875
* [MergeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/MergeSortTest.java)
876+
* [MergeSortNoExtraSpaceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/MergeSortNoExtraSpaceTest.java)
876877
* [OddEvenSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java)
877878
* [PancakeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PancakeSortTest.java)
878879
* [QuickSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/QuickSortTest.java)
880+
* [RadixSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/RadixSortTest.java)
879881
* [SelectionSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortTest.java)
880882
* [SelectionSortRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java)
881883
* [ShellSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/ShellSortTest.java)

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

+37-40
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,58 @@
22

33
import java.util.Arrays;
44

5-
final class RadixSort {
5+
/**
6+
* This class provides an implementation of the radix sort algorithm.
7+
* It sorts an array of integers in increasing order.
8+
*/
9+
public final class RadixSort {
610
private RadixSort() {
711
}
812

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-
}
13+
/**
14+
* Sorts an array of integers using the radix sort algorithm.
15+
*
16+
* @param array the array to be sorted
17+
* @return the sorted array
18+
*/
19+
public static int[] sort(int[] array) {
20+
if (array.length == 0) {
21+
return array;
1522
}
16-
return mx;
23+
24+
final int max = Arrays.stream(array).max().getAsInt();
25+
for (int exp = 1; max / exp > 0; exp *= 10) {
26+
countingSortByDigit(array, exp);
27+
}
28+
29+
return array;
1730
}
1831

19-
private static void countSort(int[] arr, int n, int exp) {
20-
int[] output = new int[n];
21-
int i;
32+
/**
33+
* A utility method to perform counting sort of array[] according to the digit represented by exp.
34+
*
35+
* @param array the array to be sorted
36+
* @param exp the exponent representing the current digit position
37+
*/
38+
private static void countingSortByDigit(int[] array, int exp) {
39+
int[] output = new int[array.length];
2240
int[] count = new int[10];
23-
Arrays.fill(count, 0);
2441

25-
for (i = 0; i < n; i++) {
26-
count[(arr[i] / exp) % 10]++;
42+
for (int i = 0; i < array.length; i++) {
43+
count[(array[i] / exp) % 10]++;
2744
}
2845

29-
for (i = 1; i < 10; i++) {
46+
for (int i = 1; i < 10; i++) {
3047
count[i] += count[i - 1];
3148
}
3249

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-
}
37-
38-
System.arraycopy(output, 0, arr, 0, n);
39-
}
40-
41-
private static void radixsort(int[] arr, int n) {
42-
int m = getMax(arr, n);
43-
44-
for (int exp = 1; m / exp > 0; exp *= 10) {
45-
countSort(arr, n, exp);
46-
}
47-
}
48-
49-
static void print(int[] arr, int n) {
50-
for (int i = 0; i < n; i++) {
51-
System.out.print(arr[i] + " ");
50+
for (int i = array.length - 1; i >= 0; i--) {
51+
int digit = (array[i] / exp) % 10;
52+
output[count[digit] - 1] = array[i];
53+
count[digit]--;
5254
}
53-
}
5455

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);
56+
System.arraycopy(output, 0, array, 0, array.length);
6057
}
6158
}
6259
// Written by James Mc Dermott(theycallmemac)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class RadixSortTest {
11+
@ParameterizedTest
12+
@MethodSource("provideTestCases")
13+
public void test(int[] inputArray, int[] expectedArray) {
14+
assertArrayEquals(RadixSort.sort(inputArray), expectedArray);
15+
}
16+
17+
private static Stream<Arguments> provideTestCases() {
18+
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}), Arguments.of(new int[] {10, 90, 49, 2, 1, 5, 23}, new int[] {1, 2, 5, 10, 23, 49, 90}),
19+
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}), 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}),
20+
Arguments.of(new int[] {1000000000, 999999999, 888888888, 777777777}, new int[] {777777777, 888888888, 999999999, 1000000000})
21+
);
22+
}
23+
}

0 commit comments

Comments
 (0)