Skip to content

Commit d9313be

Browse files
author
Alex Klymenko
committed
cleanup: BeadSort and BeadSortTest, adding javadocs
1 parent 208e1e9 commit d9313be

File tree

2 files changed

+46
-43
lines changed

2 files changed

+46
-43
lines changed

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

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
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

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+
*/
611
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+
/**
13+
* Sorts the given array using the BeadSort algorithm.
14+
*
15+
* @param array The array of non-negative integers to be sorted.
16+
* @return The sorted array.
17+
* @throws IllegalArgumentException If the array contains negative numbers.
18+
*/
19+
public int[] sort(int[] array) {
20+
if (array.length == 0) {
21+
return array;
1222
}
1323

14-
char[][] grid = new char[unsorted.length][max];
24+
if (Arrays.stream(array).anyMatch(s -> s < 0)) {
25+
throw new IllegalArgumentException("BeadSort cannot sort negative numbers.");
26+
}
27+
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];
1532
int[] count = new int[max];
1633

17-
for (int i = 0; i < unsorted.length; i++) {
34+
for (int i = 0; i < array.length; i++) {
1835
for (int j = 0; j < max; j++) {
1936
grid[i][j] = '-';
2037
}
2138
}
2239

23-
for (int i = 0; i < max; i++) {
24-
count[i] = 0;
25-
}
26-
27-
for (int i = 0; i < unsorted.length; i++) {
40+
for (int i = 0, arrayLength = array.length; i < arrayLength; i++) {
2841
int k = 0;
29-
for (int j = 0; j < unsorted[i]; j++) {
42+
for (int j = 0; j < array[i]; j++) {
3043
grid[count[max - k - 1]++][k] = '*';
3144
k++;
3245
}
3346
}
3447

35-
for (int i = 0; i < unsorted.length; i++) {
48+
for (int i = 0; i < array.length; i++) {
3649
int k = 0;
37-
for (int j = 0; j < max && grid[unsorted.length - 1 - i][j] == '*'; j++) {
50+
for (int j = 0; j < max && grid[array.length - 1 - i][j] == '*'; j++) {
3851
k++;
3952
}
4053
sorted[i] = k;
Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,32 @@
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

56
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
import java.util.stream.Stream;
612

713
public class BeadSortTest {
814
// 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-
}
1815

19-
@Test
20-
public void beadSortSingleIntegerArray() {
21-
int[] inputArray = {4};
22-
int[] outputArray = beadSort.sort(inputArray);
23-
int[] expectedOutput = {4};
24-
assertArrayEquals(outputArray, expectedOutput);
16+
@ParameterizedTest
17+
@MethodSource("provideArraysForBeadSort")
18+
public void testBeadSort(int[] inputArray, int[] expectedArray) {
19+
BeadSort beadSort = new BeadSort();
20+
assertArrayEquals(expectedArray, beadSort.sort(inputArray));
2521
}
2622

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);
23+
private static Stream<Arguments> provideArraysForBeadSort() {
24+
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}),
25+
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}));
3326
}
3427

3528
@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);
29+
public void testWithNegativeNumbers() {
30+
assertThrows(IllegalArgumentException.class, () -> new BeadSort().sort(new int[] {3, -1, 4, 1, 5, -9}));
4131
}
4232
}

0 commit comments

Comments
 (0)