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 <alx@alx.com>
Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
Alex Klymenko
2024-07-19 18:24:55 +02:00
committed by GitHub
parent f1e26064a5
commit 94032148ca
3 changed files with 107 additions and 40 deletions

View File

@ -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;
}
checkForNegativeInput(array);
radixSort(array);
return array;
}
/**
* 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.");
}
}
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);
for (i = 0; i < n; i++) {
count[(arr[i] / exp) % 10]++;
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 = 1; i < 10; i++) {
/**
* 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) {
int[] count = countDigits(array, exp);
accumulateCounts(count);
int[] output = buildOutput(array, exp, count);
copyOutput(array, output);
}
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 int getDigit(int number, int position) {
return (number / position) % BASE;
}
private static void accumulateCounts(int[] count) {
for (int i = 1; i < BASE; i++) {
count[i] += count[i - 1];
}
for (i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
System.arraycopy(output, 0, arr, 0, n);
}
private static void radixsort(int[] arr, int n) {
int m = getMax(arr, n);
for (int exp = 1; m / exp > 0; exp *= 10) {
countSort(arr, n, exp);
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;
}
static void print(int[] arr, int n) {
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
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)