Skip to content

refactor: cleanup BeadSort #5269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions src/main/java/com/thealgorithms/sorts/BeadSort.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
package com.thealgorithms.sorts;

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

/**
* BeadSort cannot sort negative numbers, characters, or strings.
* It only works for non-negative integers.
*
* @see <a href="https://en.wikipedia.org/wiki/Bead_sort">BeadSort Algorithm (Wikipedia)</a>
*/
public class BeadSort {
public int[] sort(int[] unsorted) {
int[] sorted = new int[unsorted.length];
int max = 0;
for (int i = 0; i < unsorted.length; i++) {
max = Math.max(max, unsorted[i]);
/**
* Sorts the given array using the BeadSort algorithm.
*
* @param array The array of non-negative integers to be sorted.
* @return The sorted array.
* @throws IllegalArgumentException If the array contains negative numbers.
*/
public int[] sort(int[] array) {
if (array.length == 0) {
return array;
}

char[][] grid = new char[unsorted.length][max];
if (Arrays.stream(array).anyMatch(s -> s < 0)) {
throw new IllegalArgumentException("BeadSort cannot sort negative numbers.");
}

int[] sorted = new int[array.length];
int max = Arrays.stream(array).max().orElse(0);

char[][] grid = new char[array.length][max];
int[] count = new int[max];

for (int i = 0; i < unsorted.length; i++) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < max; j++) {
grid[i][j] = '-';
}
}

for (int i = 0; i < max; i++) {
count[i] = 0;
}

for (int i = 0; i < unsorted.length; i++) {
for (int i = 0, arrayLength = array.length; i < arrayLength; i++) {
int k = 0;
for (int j = 0; j < unsorted[i]; j++) {
for (int j = 0; j < array[i]; j++) {
grid[count[max - k - 1]++][k] = '*';
k++;
}
}

for (int i = 0; i < unsorted.length; i++) {
for (int i = 0; i < array.length; i++) {
int k = 0;
for (int j = 0; j < max && grid[unsorted.length - 1 - i][j] == '*'; j++) {
for (int j = 0; j < max && grid[array.length - 1 - i][j] == '*'; j++) {
k++;
}
sorted[i] = k;
Expand Down
41 changes: 15 additions & 26 deletions src/test/java/com/thealgorithms/sorts/BeadSortTest.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
package com.thealgorithms.sorts;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class BeadSortTest {
// BeadSort can't sort negative number, Character, String. It can sort positive number only
private BeadSort beadSort = new BeadSort();

@Test
public void beadSortEmptyArray() {
int[] inputArray = {};
int[] outputArray = beadSort.sort(inputArray);
int[] expectedOutput = {};
assertArrayEquals(outputArray, expectedOutput);
@ParameterizedTest
@MethodSource("provideArraysForBeadSort")
public void testBeadSort(int[] inputArray, int[] expectedArray) {
BeadSort beadSort = new BeadSort();
assertArrayEquals(expectedArray, beadSort.sort(inputArray));
}

@Test
public void beadSortSingleIntegerArray() {
int[] inputArray = {4};
int[] outputArray = beadSort.sort(inputArray);
int[] expectedOutput = {4};
assertArrayEquals(outputArray, expectedOutput);
}

@Test
public void bogoSortNonDuplicateIntegerArray() {
int[] inputArray = {6, 1, 99, 27, 15, 23, 36};
int[] outputArray = beadSort.sort(inputArray);
int[] expectedOutput = {1, 6, 15, 23, 27, 36, 99};
assertArrayEquals(outputArray, expectedOutput);
private static Stream<Arguments> provideArraysForBeadSort() {
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}),
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}));
}

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