Skip to content

Commit ac31fba

Browse files
alxkmAlex Klymenkovil02
authored
refactor: cleanup BeadSort (#5269)
* cleanup: BeadSort and BeadSortTest, adding javadocs * checkstyle: fix formatting * checkstyle: fix import order * cleanup: improving code readability * cleanup: improving code readability using enum to represent beads * checkstyle: fix enum formatting * fix: enum should be compared using ==, according to maven bugs finder plugin --------- Co-authored-by: Alex Klymenko <[email protected]> Co-authored-by: Piotr Idzik <[email protected]>
1 parent 208e1e9 commit ac31fba

File tree

2 files changed

+56
-50
lines changed

2 files changed

+56
-50
lines changed

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

+41-22
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,59 @@
11
package com.thealgorithms.sorts;
22

3-
// BeadSort Algorithm(wikipedia) : https://en.wikipedia.org/wiki/Bead_sort
4-
// BeadSort can't sort negative number, Character, String. It can sort positive number only
3+
import java.util.Arrays;
54

65
public class BeadSort {
7-
public int[] sort(int[] unsorted) {
8-
int[] sorted = new int[unsorted.length];
9-
int max = 0;
10-
for (int i = 0; i < unsorted.length; i++) {
11-
max = Math.max(max, unsorted[i]);
12-
}
6+
private enum BeadState { BEAD, EMPTY }
137

14-
char[][] grid = new char[unsorted.length][max];
15-
int[] count = new int[max];
8+
/**
9+
* Sorts the given array using the BeadSort algorithm.
10+
*
11+
* @param array The array of non-negative integers to be sorted.
12+
* @return The sorted array.
13+
* @throws IllegalArgumentException If the array contains negative numbers.
14+
*/
15+
public int[] sort(int[] array) {
16+
allInputsMustBeNonNegative(array);
17+
return extractSortedFromGrid(fillGrid(array));
18+
}
1619

17-
for (int i = 0; i < unsorted.length; i++) {
18-
for (int j = 0; j < max; j++) {
19-
grid[i][j] = '-';
20-
}
20+
private void allInputsMustBeNonNegative(final int[] array) {
21+
if (Arrays.stream(array).anyMatch(s -> s < 0)) {
22+
throw new IllegalArgumentException("BeadSort cannot sort negative numbers.");
2123
}
24+
}
2225

23-
for (int i = 0; i < max; i++) {
24-
count[i] = 0;
25-
}
26+
private BeadState[][] fillGrid(final int[] array) {
27+
final var maxValue = Arrays.stream(array).max().orElse(0);
28+
var grid = getEmptyGrid(array.length, maxValue);
2629

27-
for (int i = 0; i < unsorted.length; i++) {
30+
int[] count = new int[maxValue];
31+
for (int i = 0, arrayLength = array.length; i < arrayLength; i++) {
2832
int k = 0;
29-
for (int j = 0; j < unsorted[i]; j++) {
30-
grid[count[max - k - 1]++][k] = '*';
33+
for (int j = 0; j < array[i]; j++) {
34+
grid[count[maxValue - k - 1]++][k] = BeadState.BEAD;
3135
k++;
3236
}
3337
}
38+
return grid;
39+
}
40+
41+
private BeadState[][] getEmptyGrid(final int arrayLength, final int maxValue) {
42+
BeadState[][] grid = new BeadState[arrayLength][maxValue];
43+
for (int i = 0; i < arrayLength; i++) {
44+
for (int j = 0; j < maxValue; j++) {
45+
grid[i][j] = BeadState.EMPTY;
46+
}
47+
}
48+
49+
return grid;
50+
}
3451

35-
for (int i = 0; i < unsorted.length; i++) {
52+
private int[] extractSortedFromGrid(final BeadState[][] grid) {
53+
int[] sorted = new int[grid.length];
54+
for (int i = 0; i < grid.length; i++) {
3655
int k = 0;
37-
for (int j = 0; j < max && grid[unsorted.length - 1 - i][j] == '*'; j++) {
56+
for (int j = 0; j < grid[grid.length - 1 - i].length && grid[grid.length - 1 - i][j] == BeadState.BEAD; j++) {
3857
k++;
3958
}
4059
sorted[i] = k;
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,29 @@
11
package com.thealgorithms.sorts;
22

33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

6+
import java.util.stream.Stream;
57
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
611

712
public class BeadSortTest {
8-
// BeadSort can't sort negative number, Character, String. It can sort positive number only
9-
private BeadSort beadSort = new BeadSort();
10-
11-
@Test
12-
public void beadSortEmptyArray() {
13-
int[] inputArray = {};
14-
int[] outputArray = beadSort.sort(inputArray);
15-
int[] expectedOutput = {};
16-
assertArrayEquals(outputArray, expectedOutput);
17-
}
18-
19-
@Test
20-
public void beadSortSingleIntegerArray() {
21-
int[] inputArray = {4};
22-
int[] outputArray = beadSort.sort(inputArray);
23-
int[] expectedOutput = {4};
24-
assertArrayEquals(outputArray, expectedOutput);
13+
@ParameterizedTest
14+
@MethodSource("provideArraysForBeadSort")
15+
public void testBeadSort(int[] inputArray, int[] expectedArray) {
16+
BeadSort beadSort = new BeadSort();
17+
assertArrayEquals(expectedArray, beadSort.sort(inputArray));
2518
}
2619

27-
@Test
28-
public void bogoSortNonDuplicateIntegerArray() {
29-
int[] inputArray = {6, 1, 99, 27, 15, 23, 36};
30-
int[] outputArray = beadSort.sort(inputArray);
31-
int[] expectedOutput = {1, 6, 15, 23, 27, 36, 99};
32-
assertArrayEquals(outputArray, expectedOutput);
20+
private static Stream<Arguments> provideArraysForBeadSort() {
21+
return Stream.of(Arguments.of(new int[] {}, new int[] {}), Arguments.of(new int[] {4}, new int[] {4}), Arguments.of(new int[] {6, 1, 99, 27, 15, 23, 36}, new int[] {1, 6, 15, 23, 27, 36, 99}), Arguments.of(new int[] {6, 1, 27, 15, 23, 27, 36, 23}, new int[] {1, 6, 15, 23, 23, 27, 27, 36}),
22+
Arguments.of(new int[] {5, 5, 5, 5, 5}, new int[] {5, 5, 5, 5, 5}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {1, 2, 3, 4, 5}));
3323
}
3424

3525
@Test
36-
public void bogoSortDuplicateIntegerArray() {
37-
int[] inputArray = {6, 1, 27, 15, 23, 27, 36, 23};
38-
int[] outputArray = beadSort.sort(inputArray);
39-
int[] expectedOutput = {1, 6, 15, 23, 23, 27, 27, 36};
40-
assertArrayEquals(outputArray, expectedOutput);
26+
public void testWithNegativeNumbers() {
27+
assertThrows(IllegalArgumentException.class, () -> new BeadSort().sort(new int[] {3, 1, 4, 1, 5, -9}));
4128
}
4229
}

0 commit comments

Comments
 (0)