Skip to content

Commit f3555ac

Browse files
author
Alex Klymenko
committed
cleanup: improving code readability
1 parent 478661f commit f3555ac

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
import java.util.Arrays;
44

5-
/**
6-
* BeadSort cannot sort negative numbers, characters, or strings.
7-
* It only works for non-negative integers.
8-
*
9-
* @see <a href="https://en.wikipedia.org/wiki/Bead_sort">BeadSort Algorithm (Wikipedia)</a>
10-
*/
115
public class BeadSort {
126
/**
137
* Sorts the given array using the BeadSort algorithm.
@@ -17,37 +11,47 @@ public class BeadSort {
1711
* @throws IllegalArgumentException If the array contains negative numbers.
1812
*/
1913
public int[] sort(int[] array) {
20-
if (array.length == 0) {
21-
return array;
22-
}
14+
allInputsMustBeNonNegative(array);
15+
return extractSortedFromGrid(fillGrid(array));
16+
}
2317

18+
private void allInputsMustBeNonNegative(final int[] array) {
2419
if (Arrays.stream(array).anyMatch(s -> s < 0)) {
2520
throw new IllegalArgumentException("BeadSort cannot sort negative numbers.");
2621
}
22+
}
2723

28-
int[] sorted = new int[array.length];
29-
int max = Arrays.stream(array).max().orElse(0);
30-
31-
char[][] grid = new char[array.length][max];
32-
int[] count = new int[max];
33-
34-
for (int i = 0; i < array.length; i++) {
35-
for (int j = 0; j < max; j++) {
36-
grid[i][j] = '-';
37-
}
38-
}
24+
private boolean[][] fillGrid(final int[] array) {
25+
final var maxValue = Arrays.stream(array).max().orElse(0);
26+
var grid = getEmptyGrid(array.length, maxValue);
3927

28+
int[] count = new int[maxValue];
4029
for (int i = 0, arrayLength = array.length; i < arrayLength; i++) {
4130
int k = 0;
4231
for (int j = 0; j < array[i]; j++) {
43-
grid[count[max - k - 1]++][k] = '*';
32+
grid[count[maxValue - k - 1]++][k] = true;
4433
k++;
4534
}
4635
}
36+
return grid;
37+
}
38+
39+
private boolean[][] getEmptyGrid(final int arrayLength, final int maxValue) {
40+
boolean[][] grid = new boolean[arrayLength][maxValue];
41+
for (int i = 0; i < arrayLength; i++) {
42+
for (int j = 0; j < maxValue; j++) {
43+
grid[i][j] = false;
44+
}
45+
}
46+
47+
return grid;
48+
}
4749

48-
for (int i = 0; i < array.length; i++) {
50+
private int[] extractSortedFromGrid(final boolean[][] grid) {
51+
int[] sorted = new int[grid.length];
52+
for (int i = 0; i < grid.length; i++) {
4953
int k = 0;
50-
for (int j = 0; j < max && grid[array.length - 1 - i][j] == '*'; j++) {
54+
for (int j = 0; j < grid[grid.length - 1 - i].length && grid[grid.length - 1 - i][j]; j++) {
5155
k++;
5256
}
5357
sorted[i] = k;

src/test/java/com/thealgorithms/sorts/BeadSortTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import org.junit.jupiter.params.provider.MethodSource;
1111

1212
public class BeadSortTest {
13-
// BeadSort can't sort negative number, Character, String. It can sort positive number only
14-
1513
@ParameterizedTest
1614
@MethodSource("provideArraysForBeadSort")
1715
public void testBeadSort(int[] inputArray, int[] expectedArray) {
@@ -26,6 +24,6 @@ private static Stream<Arguments> provideArraysForBeadSort() {
2624

2725
@Test
2826
public void testWithNegativeNumbers() {
29-
assertThrows(IllegalArgumentException.class, () -> new BeadSort().sort(new int[] {3, -1, 4, 1, 5, -9}));
27+
assertThrows(IllegalArgumentException.class, () -> new BeadSort().sort(new int[] {3, 1, 4, 1, 5, -9}));
3028
}
3129
}

0 commit comments

Comments
 (0)