Repository: TheAlgorithms/Java
Pull request #4205 (2023-05-28 10:07:19)

style: handle empty input array in `FindMin.findMin`
<!-- For completed items, change [ ] to [x] -->
The current implementation of the method [`findMin`](https://github.com/TheAlgorithms/Java/blob/e14b30b88c0de95186fe1937f795b7d8fd3fa8aa/src/main/java/com/thealgorithms/maths/FindMin.java#L32) has some issues:
- all of the [existing test cases](https://github.com/TheAlgorithms/Java/blob/e14b30b88c0de95186fe1937f795b7d8fd3fa8aa/src/test/java/com/thealgorithms/maths/FindMinTest.java) have the minimum at index 0,
- it crashed for empty input.

In this PR, I:
- added missing test cases 982b296e1ffffb5042c05d018f2a94743d8c88ff,
- make `findMin` throw `IllegalArgumentException` when input is empty 548e1aa1043b337da160974403ab03a0ad9c9c67,
- do some minor style changes and update the documentation.

As in #4199 I decided not to create a separate issue for this - I hope this is not a problem.

- [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.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/FindMin.java b/src/main/java/com/thealgorithms/maths/FindMin.java
index e3be09e3..7764c1c0 100644
--- a/src/main/java/com/thealgorithms/maths/FindMin.java
+++ b/src/main/java/com/thealgorithms/maths/FindMin.java
@@ -24,16 +24,20 @@ public class FindMin {
     }
 
     /**
-     * Find the minimum number of an array of numbers.
+     * @brief finds the minimum value stored in the input array
      *
-     * @param array the array contains element
-     * @return min value
+     * @param array the input array
+     * @exception IllegalArgumentException input array is empty
+     * @return the mimum value stored in the input array
      */
     public static int findMin(int[] array) {
-        int min = array[0];
-        for (int i = 1; i < array.length; ++i) {
-            if (array[i] < min) {
-                min = array[i];
+        if (array.length == 0) {
+            throw new IllegalArgumentException("array must be non-empty.");
+        }
+        int min = Integer.MAX_VALUE;
+        for (final var value : array) {
+            if (value < min) {
+                min = value;
             }
         }
         return min;


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/FindMinTest.java b/src/test/java/com/thealgorithms/maths/FindMinTest.java
index 48fcb277..dc983547 100644
--- a/src/test/java/com/thealgorithms/maths/FindMinTest.java
+++ b/src/test/java/com/thealgorithms/maths/FindMinTest.java
@@ -1,6 +1,7 @@
 package com.thealgorithms.maths;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import org.junit.jupiter.api.Test;
 
@@ -23,4 +24,17 @@ public class FindMinTest {
     public void test2() {
         assertEquals(0, FindMin.findMin(new int[] { 0, 192, 384, 576 }));
     }
+
+    @Test
+    public void test3() {
+        assertEquals(0, FindMin.findMin(new int[] { 10, 10, 0, 10 }));
+    }
+
+    @Test
+    public void testFindMinThrowsExceptionForEmptyInput() {
+        assertThrows(
+            IllegalArgumentException.class,
+            () -> FindMin.findMin(new int[]{})
+        );
+    }
 }