Repository: TheAlgorithms/Java
Pull request #3761 (2022-11-08 02:21:56)

Create Bead sort and testcase
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.

#3718 


Patch:
diff --git a/src/main/java/com/thealgorithms/sorts/BeadSort.java b/src/main/java/com/thealgorithms/sorts/BeadSort.java
new file mode 100644
index 00000000..dd064b4f
--- /dev/null
+++ b/src/main/java/com/thealgorithms/sorts/BeadSort.java
@@ -0,0 +1,45 @@
+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
+
+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]);
+    }
+
+    char[][] grid = new char[unsorted.length][max];
+    int[] count = new int[max];
+
+    for(int i = 0; i < unsorted.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++) {
+            int k = 0;
+            for(int j = 0; j < (int) unsorted[i] ; j++) {
+                grid[count[max - k - 1]++][k] = '*';
+                k++;
+             }
+        }
+
+        for(int i = 0; i < unsorted.length; i++) {
+            int k = 0;
+            for(int j = 0; j < max && grid[unsorted.length - 1 - i][j] == '*'; j++) {
+                k++;
+            }
+            sorted[i] = k;
+        }
+        return sorted;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/sorts/BeadSortTest.java b/src/test/java/com/thealgorithms/sorts/BeadSortTest.java
new file mode 100644
index 00000000..05f036ed
--- /dev/null
+++ b/src/test/java/com/thealgorithms/sorts/BeadSortTest.java
@@ -0,0 +1,42 @@
+package com.thealgorithms.sorts;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+import org.junit.jupiter.api.Test;
+
+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);
+    }
+
+    @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);
+    }
+    
+    @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);
+    }
+}
\ No newline at end of file