5
5
6
6
/**
7
7
* 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.
9
9
*/
10
10
public final class RadixSort {
11
11
private static final int BASE = 10 ;
@@ -14,7 +14,7 @@ private RadixSort() {
14
14
}
15
15
16
16
/**
17
- * Sorts an array of positive integers using the radix sort algorithm.
17
+ * Sorts an array of nonnegative integers using the radix sort algorithm.
18
18
*
19
19
* @param array the array to be sorted
20
20
* @return the sorted array
@@ -58,19 +58,18 @@ private static void radixSort(int[] array) {
58
58
* @param exp the exponent representing the current digit position
59
59
*/
60
60
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 );
65
62
accumulateCounts (count );
66
- buildOutput (array , exp , output , count );
63
+ int [] output = buildOutput (array , exp , count );
67
64
copyOutput (array , output );
68
65
}
69
66
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 ];
71
69
for (int i = 0 ; i < array .length ; i ++) {
72
70
count [getDigit (array [i ], exp )]++;
73
71
}
72
+ return count ;
74
73
}
75
74
76
75
private static int getDigit (int number , int position ) {
@@ -83,12 +82,14 @@ private static void accumulateCounts(int[] count) {
83
82
}
84
83
}
85
84
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 ];
87
87
for (int i = array .length - 1 ; i >= 0 ; i --) {
88
88
int digit = getDigit (array [i ], exp );
89
89
output [count [digit ] - 1 ] = array [i ];
90
90
count [digit ]--;
91
91
}
92
+ return output ;
92
93
}
93
94
94
95
private static void copyOutput (int [] array , int [] output ) {
0 commit comments