JavaAlgorithms/Maths/FindMin.java

40 lines
823 B
Java
Raw Normal View History

2019-09-28 11:15:22 +08:00
package Maths;
2020-08-23 12:40:30 +08:00
import java.util.Arrays;
import java.util.Random;
2019-09-28 11:15:22 +08:00
public class FindMin {
2020-10-24 18:23:28 +08:00
/** Driver Code */
public static void main(String[] args) {
Random random = new Random();
2020-08-23 12:40:30 +08:00
2020-10-24 18:23:28 +08:00
/* random size */
int size = random.nextInt(100) + 1;
int[] array = new int[size];
2020-08-23 12:40:30 +08:00
2020-10-24 18:23:28 +08:00
/* init array with random numbers */
for (int i = 0; i < size; i++) {
array[i] = random.nextInt() % 100;
2019-09-28 11:15:22 +08:00
}
2020-10-24 18:23:28 +08:00
assert Arrays.stream(array).min().getAsInt() == findMin(array);
}
/**
* Find the minimum number of an array of numbers.
*
* @param array the array contains element
* @return min value
*/
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];
}
2019-09-28 11:15:22 +08:00
}
2020-10-24 18:23:28 +08:00
return min;
}
2019-09-28 11:15:22 +08:00
}