-
Notifications
You must be signed in to change notification settings - Fork 20k
refactor: cleanup RadixSort
#5280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b218afa
refactor: refactoring RadixSort, adding test, update DIRECTORY.md
a70066c
checkstyle: fix formatting for test
790d66c
Merge branch 'master' into refactor/radix_sort
alxkm 10de932
Merge branch 'master' into refactor/radix_sort
alxkm b069cd7
Merge branch 'master' into refactor/radix_sort
alxkm dd3825b
Merge branch 'master' into refactor/radix_sort
alxkm 1a20a90
Merge branch 'master' into refactor/radix_sort
alxkm ed7a199
refactor: adding possibility to sort negative numbers. Improve tests.…
7bc5b85
checkstyle: fix formatting
120be37
Merge branch 'refs/heads/master' into refactor/radix_sort
ba012f0
refactor: resolve conflicts with master branch
30d898a
refactor: remove negative integers support
6690617
checkstyle: fix formatting
505a2f2
checkstyle: fix formatting, revert test
4c4ad5d
Merge branch 'master' into refactor/radix_sort
alxkm 0444962
refactor: adding return array to countDigits and buildOutput method, …
e2d2580
Merge remote-tracking branch 'origin/refactor/radix_sort' into refact…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,98 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
import com.thealgorithms.maths.NumberOfDigits; | ||
import java.util.Arrays; | ||
|
||
final class RadixSort { | ||
/** | ||
* This class provides an implementation of the radix sort algorithm. | ||
* It sorts an array of nonnegative integers in increasing order. | ||
*/ | ||
public final class RadixSort { | ||
private static final int BASE = 10; | ||
|
||
private RadixSort() { | ||
} | ||
|
||
private static int getMax(int[] arr, int n) { | ||
int mx = arr[0]; | ||
for (int i = 1; i < n; i++) { | ||
if (arr[i] > mx) { | ||
mx = arr[i]; | ||
} | ||
/** | ||
* Sorts an array of nonnegative integers using the radix sort algorithm. | ||
* | ||
* @param array the array to be sorted | ||
* @return the sorted array | ||
* @throws IllegalArgumentException if any negative integers are found | ||
*/ | ||
public static int[] sort(int[] array) { | ||
if (array.length == 0) { | ||
return array; | ||
} | ||
return mx; | ||
} | ||
|
||
private static void countSort(int[] arr, int n, int exp) { | ||
int[] output = new int[n]; | ||
int i; | ||
int[] count = new int[10]; | ||
Arrays.fill(count, 0); | ||
checkForNegativeInput(array); | ||
radixSort(array); | ||
return array; | ||
} | ||
|
||
for (i = 0; i < n; i++) { | ||
count[(arr[i] / exp) % 10]++; | ||
/** | ||
* Checks if the array contains any negative integers. | ||
* | ||
* @param array the array to be checked | ||
* @throws IllegalArgumentException if any negative integers are found | ||
*/ | ||
private static void checkForNegativeInput(int[] array) { | ||
for (int number : array) { | ||
if (number < 0) { | ||
throw new IllegalArgumentException("Array contains non-positive integers."); | ||
} | ||
} | ||
} | ||
|
||
for (i = 1; i < 10; i++) { | ||
count[i] += count[i - 1]; | ||
private static void radixSort(int[] array) { | ||
final int max = Arrays.stream(array).max().getAsInt(); | ||
for (int i = 0, exp = 1; i < NumberOfDigits.numberOfDigits(max); i++, exp *= BASE) { | ||
countingSortByDigit(array, exp); | ||
} | ||
} | ||
|
||
for (i = n - 1; i >= 0; i--) { | ||
output[count[(arr[i] / exp) % 10] - 1] = arr[i]; | ||
count[(arr[i] / exp) % 10]--; | ||
} | ||
/** | ||
* A utility method to perform counting sort of array[] according to the digit represented by exp. | ||
* | ||
* @param array the array to be sorted | ||
* @param exp the exponent representing the current digit position | ||
*/ | ||
private static void countingSortByDigit(int[] array, int exp) { | ||
alxkm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int[] count = countDigits(array, exp); | ||
accumulateCounts(count); | ||
int[] output = buildOutput(array, exp, count); | ||
copyOutput(array, output); | ||
} | ||
|
||
System.arraycopy(output, 0, arr, 0, n); | ||
private static int[] countDigits(int[] array, int exp) { | ||
int[] count = new int[BASE]; | ||
for (int i = 0; i < array.length; i++) { | ||
count[getDigit(array[i], exp)]++; | ||
} | ||
return count; | ||
} | ||
|
||
private static void radixsort(int[] arr, int n) { | ||
int m = getMax(arr, n); | ||
private static int getDigit(int number, int position) { | ||
return (number / position) % BASE; | ||
} | ||
|
||
for (int exp = 1; m / exp > 0; exp *= 10) { | ||
countSort(arr, n, exp); | ||
private static void accumulateCounts(int[] count) { | ||
for (int i = 1; i < BASE; i++) { | ||
count[i] += count[i - 1]; | ||
} | ||
} | ||
|
||
static void print(int[] arr, int n) { | ||
for (int i = 0; i < n; i++) { | ||
System.out.print(arr[i] + " "); | ||
private static int[] buildOutput(int[] array, int exp, int[] count) { | ||
int[] output = new int[array.length]; | ||
for (int i = array.length - 1; i >= 0; i--) { | ||
int digit = getDigit(array[i], exp); | ||
output[count[digit] - 1] = array[i]; | ||
count[digit]--; | ||
} | ||
return output; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] arr = {170, 45, 75, 90, 802, 24, 2, 66}; | ||
int n = arr.length; | ||
radixsort(arr, n); | ||
print(arr, n); | ||
private static void copyOutput(int[] array, int[] output) { | ||
System.arraycopy(output, 0, array, 0, array.length); | ||
} | ||
} | ||
// Written by James Mc Dermott(theycallmemac) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
public class RadixSortTest { | ||
@ParameterizedTest | ||
@MethodSource("provideTestCases") | ||
public void test(int[] inputArray, int[] expectedArray) { | ||
assertArrayEquals(RadixSort.sort(inputArray), expectedArray); | ||
} | ||
|
||
private static Stream<Arguments> provideTestCases() { | ||
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}), 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}), | ||
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})); | ||
} | ||
|
||
@Test | ||
public void testWithNegativeNumbers() { | ||
assertThrows(IllegalArgumentException.class, () -> RadixSort.sort(new int[] {3, 1, 4, 1, 5, -9})); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.