Make FindMin a proper utilty class (#4397)

This commit is contained in:
Piotr Idzik 2023-09-24 12:25:28 +02:00 committed by GitHub
parent fa77b50ef9
commit d6024f9cd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,26 +1,7 @@
package com.thealgorithms.maths; package com.thealgorithms.maths;
import java.util.Arrays; public final class FindMin {
import java.util.Random; private FindMin() {
public class FindMin {
/**
* Driver Code
*/
public static void main(String[] args) {
Random random = new Random();
/* random size */
int size = random.nextInt(100) + 1;
int[] array = new int[size];
/* init array with random numbers */
for (int i = 0; i < size; i++) {
array[i] = random.nextInt() % 100;
}
assert Arrays.stream(array).min().getAsInt() == findMin(array);
} }
/** /**
@ -30,7 +11,7 @@ public class FindMin {
* @exception IllegalArgumentException input array is empty * @exception IllegalArgumentException input array is empty
* @return the mimum value stored in the input array * @return the mimum value stored in the input array
*/ */
public static int findMin(int[] array) { public static int findMin(final int[] array) {
if (array.length == 0) { if (array.length == 0) {
throw new IllegalArgumentException("array must be non-empty."); throw new IllegalArgumentException("array must be non-empty.");
} }