Skip to content

Commit 6d8381b

Browse files
committed
fix: checkstyle errors
1 parent 3474233 commit 6d8381b

File tree

2 files changed

+66
-50
lines changed

2 files changed

+66
-50
lines changed
Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,69 @@
11
package algorithms.sorting.radixSort;
22

33
/**
4-
* Implementation of Radix sort.
5-
* O((b/r)*(N + 2^r))
6-
* where N is the number of integers,
7-
* b is the total number of bits (32 bits for int),
8-
* and r is the number of bits for each segment.
9-
* Space: O(N) auxiliary space.
10-
*/
4+
* This class implements a Radix Sort Algorithm.
5+
*/
116
public class RadixSort {
12-
private static final int NUM_BITS = 8;
13-
private static final int NUM_SEGMENTS = 4;
147

15-
private static int getSegmentMasked(int num, int segment) {
16-
int mask = ((1 << NUM_BITS) - 1) << (segment * NUM_BITS);
17-
return (num & mask) >> (segment * NUM_BITS);
18-
}
8+
private static final int NUM_BITS = 8;
9+
private static final int NUM_SEGMENTS = 4;
1910

20-
private static void radixSort(int[] arr, int[] sorted) {
21-
// sort the N numbers by segments, starting from left-most segment
22-
for (int i = 0; i < NUM_SEGMENTS; i++) {
23-
int[] freqMap = new int[1 << NUM_BITS]; // at most this number of elements
11+
/**
12+
* Creates masking on the segment to obtain the value of the digit.
13+
*
14+
* @param num The number.
15+
* @param segment The segment we are interested in.
16+
*
17+
* @return The value of the digit in the number at the given segment.
18+
*/
19+
private static int getSegmentMasked(int num, int segment) {
20+
int mask = ((1 << NUM_BITS) - 1) << (segment * NUM_BITS);
21+
return (num & mask) >> (segment * NUM_BITS);
22+
}
2423

25-
// count each element
26-
for (int num : arr) {
27-
freqMap[getSegmentMasked(num, i)]++;
28-
}
29-
// get prefix sum
30-
for (int j = 1; j < freqMap.length; j++) {
31-
freqMap[j] += freqMap[j-1];
32-
}
33-
// place each number in its correct sorted position up until the given segment
34-
for (int k = arr.length-1; k >= 0; k--) {
35-
int curr = arr[k];
36-
int id = getSegmentMasked(curr, i);
37-
sorted[freqMap[id] - 1] = curr;
38-
freqMap[id]--;
39-
}
40-
// we do a swap so that our results above for this segment is saved and passed as input to the next segment
41-
int[] tmp = arr;
42-
arr = sorted;
43-
sorted = tmp;
44-
}
45-
sorted = arr;
46-
}
24+
/**
25+
* Radix sorts a given input array and updates the output array in-place.
26+
*
27+
* @param arr original input array.
28+
* @param sorted output array.
29+
*/
30+
private static void radixSort(int[] arr, int[] sorted) {
31+
// sort the N numbers by segments, starting from left-most segment
32+
for (int i = 0; i < NUM_SEGMENTS; i++) {
33+
int[] freqMap = new int[1 << NUM_BITS]; // at most this number of elements
4734

48-
public static void radixSort(int[] arr) {
49-
int[] sorted = new int[arr.length];
50-
radixSort(arr, sorted);
51-
arr = sorted; // swap back lol
35+
// count each element
36+
for (int num : arr) {
37+
freqMap[getSegmentMasked(num, i)]++;
38+
}
39+
// get prefix sum
40+
for (int j = 1; j < freqMap.length; j++) {
41+
freqMap[j] += freqMap[j - 1];
42+
}
43+
// place each number in its correct sorted position up until the given segment
44+
for (int k = arr.length - 1; k >= 0; k--) {
45+
int curr = arr[k];
46+
int id = getSegmentMasked(curr, i);
47+
sorted[freqMap[id] - 1] = curr;
48+
freqMap[id]--;
49+
}
50+
// we do a swap so that our results above for this segment is
51+
// saved and passed as input to the next segment
52+
int[] tmp = arr;
53+
arr = sorted;
54+
sorted = tmp;
5255
}
56+
sorted = arr;
57+
}
58+
59+
/**
60+
* Calls radix sort inplace on a given array.
61+
*
62+
* @param arr The array to be sorted.
63+
*/
64+
public static void radixSort(int[] arr) {
65+
int[] sorted = new int[arr.length];
66+
radixSort(arr, sorted);
67+
arr = sorted; // swap back lol
68+
}
5369
}

src/test/java/algorithms/sorting/radixSort/RadixSortTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package algorithms.sorting.radixSort;
22

33
import static org.junit.Assert.assertArrayEquals;
4-
import org.junit.Test;
4+
55
import java.util.Arrays;
6+
import org.junit.Test;
7+
68
public class RadixSortTest {
79
@Test
810
public void test_radixSort_shouldReturnSortedArray() {
9-
int[] firstArray =
10-
new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2,
11+
int[] firstArray = new int[] {2, 3, 4, 1, 2, 5, 6, 7, 10, 15, 20, 13, 15, 1, 2,
1112
15, 12, 20, 21, 120, 11, 5, 7, 85, 30};
1213
int[] firstResult = Arrays.copyOf(firstArray, firstArray.length);
1314
RadixSort.radixSort(firstResult);
1415

15-
int[] secondArray
16-
= new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4,
17-
3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
18-
int[] secondResult =Arrays.copyOf(secondArray, secondArray.length);
16+
int[] secondArray = new int[] {9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 9, 8, 7, 6, 5, 4,
17+
3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
18+
int[] secondResult = Arrays.copyOf(secondArray, secondArray.length);
1919
RadixSort.radixSort(secondResult);
2020

2121
int[] thirdArray = new int[] {};

0 commit comments

Comments
 (0)