Repository: TheAlgorithms/Java
Pull request #4568 (2023-10-02 16:20:58)

cleanup `FindMinRecursion`
#4431 for FindMinRecursionTest 

- [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.


Shobhit0603: sir can i commit the changes?


Patch:
diff --git a/src/main/java/com/thealgorithms/maths/FindMinRecursion.java b/src/main/java/com/thealgorithms/maths/FindMinRecursion.java
index 71d7795b..6ffe034e 100644
--- a/src/main/java/com/thealgorithms/maths/FindMinRecursion.java
+++ b/src/main/java/com/thealgorithms/maths/FindMinRecursion.java
@@ -1,38 +1,13 @@
 package com.thealgorithms.maths;
 
-import java.util.Arrays;
-import java.util.Random;
+public final class FindMinRecursion {
 
-public class FindMinRecursion {
-
-    /**
-     * Driver Code
-     */
-    public static void main(String[] args) {
-        Random rand = new Random();
-
-        /* rand size */
-        int size = rand.nextInt(100) + 1;
-        int[] array = new int[size];
-
-        /* init array with rand numbers */
-        for (int i = 0; i < size; i++) {
-            array[i] = rand.nextInt() % 100;
-        }
-
-        assert min(array, 0, array.length - 1) == Arrays.stream(array).min().getAsInt();
-        assert min(array) == Arrays.stream(array).min().getAsInt();
+    private FindMinRecursion() {
     }
-
-    /**
-     * Get min of array using divide and conquer algorithm
-     *
-     * @param array contains elements
-     * @param low the index of the first element
-     * @param high the index of the last element
-     * @return min of {@code array}
-     */
-    public static int min(int[] array, int low, int high) {
+    public static int min(final int[] array, final int low, final int high) {
+        if (array.length == 0) {
+            throw new IllegalArgumentException("array must be non-empty.");
+        }
         if (low == high) {
             return array[low]; // or array[high]
         }
@@ -52,7 +27,7 @@ public class FindMinRecursion {
      * @param len length of given array
      * @return min value of {@code array}
      */
-    public static int min(int[] array) {
-        return array.length == 1 ? array[0] : min(array, 0, array.length);
+    public static int min(final int[] array) {
+        return min(array, 0, array.length - 1);
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/FindMinRecursionTest.java b/src/test/java/com/thealgorithms/maths/FindMinRecursionTest.java
new file mode 100644
index 00000000..3c36702b
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/FindMinRecursionTest.java
@@ -0,0 +1,28 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.stream.Stream;
+import org.junit.jupiter.api.Assertions;
+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 FindMinRecursionTest {
+
+    @ParameterizedTest
+    @MethodSource("inputStream")
+    void numberTests(int expected, int[] input) {
+        Assertions.assertEquals(expected, FindMinRecursion.min(input));
+    }
+
+    private static Stream<Arguments> inputStream() {
+        return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(-1, new int[] {-1, 0}), Arguments.of(-10, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(-4, new int[] {3, -2, 3, 9, -4, -4, 8}), Arguments.of(3, new int[] {3}));
+    }
+
+    @Test
+    public void testFindMaxThrowsExceptionForEmptyInput() {
+        assertThrows(IllegalArgumentException.class, () -> FindMinRecursion.min(new int[] {}));
+    }
+}