Skip to content

Commit 30d898a

Browse files
author
alxklm
committed
refactor: remove negative integers support
1 parent ba012f0 commit 30d898a

File tree

2 files changed

+28
-36
lines changed

2 files changed

+28
-36
lines changed

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

+19-32
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.thealgorithms.sorts;
22

33
import com.thealgorithms.maths.NumberOfDigits;
4+
45
import java.util.Arrays;
56

67
/**
78
* This class provides an implementation of the radix sort algorithm.
8-
* It sorts an array of integers in increasing order.
9+
* It sorts an array of positive integers in increasing order.
910
*/
1011
public final class RadixSort {
1112
private static final int BASE = 10;
@@ -14,37 +15,34 @@ private RadixSort() {
1415
}
1516

1617
/**
17-
* Sorts an array of integers using the radix sort algorithm.
18+
* Sorts an array of positive integers using the radix sort algorithm.
1819
*
1920
* @param array the array to be sorted
2021
* @return the sorted array
22+
* @throws IllegalArgumentException if any negative integers are found
2123
*/
2224
public static int[] sort(int[] array) {
2325
if (array.length == 0) {
2426
return array;
2527
}
2628

27-
int[] negatives = Arrays.stream(array).filter(x -> x < 0).map(x -> - x).toArray();
28-
int[] positives = Arrays.stream(array).filter(x -> x >= 0).toArray();
29-
30-
if (negatives.length > 0) {
31-
radixSort(negatives);
32-
reverseAndNegate(negatives);
33-
}
34-
35-
if (positives.length > 0) {
36-
radixSort(positives);
37-
}
38-
39-
int[] sortedArray = new int[array.length];
40-
mergeNegativeAndPositiveArrays(negatives, sortedArray, positives);
41-
42-
return sortedArray;
29+
checkForNegativeInput(array);
30+
radixSort(array);
31+
return array;
4332
}
4433

45-
private static void mergeNegativeAndPositiveArrays(int[] negatives, int[] sortedArray, int[] positives) {
46-
System.arraycopy(negatives, 0, sortedArray, 0, negatives.length);
47-
System.arraycopy(positives, 0, sortedArray, negatives.length, positives.length);
34+
/**
35+
* Checks if the array contains any negative integers.
36+
*
37+
* @param array the array to be checked
38+
* @throws IllegalArgumentException if any negative integers are found
39+
*/
40+
private static void checkForNegativeInput(int[] array) {
41+
for (int number : array) {
42+
if (number < 0) {
43+
throw new IllegalArgumentException("Array contains non-positive integers.");
44+
}
45+
}
4846
}
4947

5048
private static void radixSort(int[] array) {
@@ -54,17 +52,6 @@ private static void radixSort(int[] array) {
5452
}
5553
}
5654

57-
private static void reverseAndNegate(int[] array) {
58-
for (int i = 0; i < array.length / 2; i++) {
59-
int temp = array[i];
60-
array[i] = -array[array.length - 1 - i];
61-
array[array.length - 1 - i] = -temp;
62-
}
63-
if (array.length % 2 != 0) {
64-
array[array.length / 2] = -array[array.length / 2];
65-
}
66-
}
67-
6855
/**
6956
* A utility method to perform counting sort of array[] according to the digit represented by exp.
7057
*

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.thealgorithms.sorts;
22

33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.util.stream.Stream;
7+
8+
import org.junit.jupiter.api.Test;
69
import org.junit.jupiter.params.ParameterizedTest;
710
import org.junit.jupiter.params.provider.Arguments;
811
import org.junit.jupiter.params.provider.MethodSource;
@@ -18,9 +21,11 @@ private static Stream<Arguments> provideTestCases() {
1821
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}),
1922
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}),
2023
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}),
21-
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}),
22-
Arguments.of(new int[] {-170, 45, -75, 90, -802, 24, -2, 66}, new int[] {-802, -170, -75, -2, 24, 45, 66, 90}), 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[] {-14, -9, -8, -6, -4, -3}),
23-
Arguments.of(new int[] {10, -90, 49, -2, 1, -5, 23}, new int[] {-90, -5, -2, 1, 10, 23, 49}), Arguments.of(new int[] {-1, -3, -4, -2, -7, -8}, new int[] {-8, -7, -4, -3, -2, -1}), Arguments.of(new int[] {1, -1, 0, -100, 100}, new int[] {-100, -1, 0, 1, 100}),
24-
Arguments.of(new int[] {-1000000000, 999999999, -888888888, 777777777}, new int[] {-1000000000, -888888888, 777777777, 999999999}), Arguments.of(new int[] {-123, 9, -54321, 123456789, 0}, new int[] {-54321, -123, 0, 9, 123456789}));
24+
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}));
25+
}
26+
27+
@Test
28+
public void testWithNegativeNumbers() {
29+
assertThrows(IllegalArgumentException.class, () -> RadixSort.sort(new int[] {3, 1, 4, 1, 5, -9}));
2530
}
2631
}

0 commit comments

Comments
 (0)