Skip to content

Commit 0444962

Browse files
author
alxklm
committed
refactor: adding return array to countDigits and buildOutput method, adding more specific description to javadocs
1 parent 505a2f2 commit 0444962

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/**
77
* This class provides an implementation of the radix sort algorithm.
8-
* It sorts an array of positive integers in increasing order.
8+
* It sorts an array of nonnegative integers in increasing order.
99
*/
1010
public final class RadixSort {
1111
private static final int BASE = 10;
@@ -14,7 +14,7 @@ private RadixSort() {
1414
}
1515

1616
/**
17-
* Sorts an array of positive integers using the radix sort algorithm.
17+
* Sorts an array of nonnegative integers using the radix sort algorithm.
1818
*
1919
* @param array the array to be sorted
2020
* @return the sorted array
@@ -58,19 +58,18 @@ private static void radixSort(int[] array) {
5858
* @param exp the exponent representing the current digit position
5959
*/
6060
private static void countingSortByDigit(int[] array, int exp) {
61-
int[] output = new int[array.length];
62-
int[] count = new int[BASE];
63-
64-
countDigits(array, exp, count);
61+
int[] count = countDigits(array, exp);
6562
accumulateCounts(count);
66-
buildOutput(array, exp, output, count);
63+
int[] output = buildOutput(array, exp, count);
6764
copyOutput(array, output);
6865
}
6966

70-
private static void countDigits(int[] array, int exp, int[] count) {
67+
private static int[] countDigits(int[] array, int exp) {
68+
int[] count = new int[BASE];
7169
for (int i = 0; i < array.length; i++) {
7270
count[getDigit(array[i], exp)]++;
7371
}
72+
return count;
7473
}
7574

7675
private static int getDigit(int number, int position) {
@@ -83,12 +82,14 @@ private static void accumulateCounts(int[] count) {
8382
}
8483
}
8584

86-
private static void buildOutput(int[] array, int exp, int[] output, int[] count) {
85+
private static int[] buildOutput(int[] array, int exp, int[] count) {
86+
int[] output = new int[array.length];
8787
for (int i = array.length - 1; i >= 0; i--) {
8888
int digit = getDigit(array[i], exp);
8989
output[count[digit] - 1] = array[i];
9090
count[digit]--;
9191
}
92+
return output;
9293
}
9394

9495
private static void copyOutput(int[] array, int[] output) {

0 commit comments

Comments
 (0)