Files
Java/src/main/java/com/thealgorithms/maths/FindMinRecursion.java
Abhinav Pandey 152e29034d Improved code readability and code quality (#4663)
* Fixed Small typos :-)

* Update BufferedReader.java

* Made the following changes :

* Improved readability of files and removed gramatical errors.

* Implemented data assigning instead of manually calling arr.ylength in several instances like FindMax, FindMaxRecursion etc.

* Removed unwanted params from several files

* Implemented Math methods in files math/FindMinRecursion.java and FindMaxRecursion.java

* Update src/main/java/com/thealgorithms/maths/FindMinRecursion.java

---------

Co-authored-by: Debasish Biswas <debasishbsws.dev@gmail.com>
2023-10-11 17:29:55 +05:30

43 lines
1.1 KiB
Java

package com.thealgorithms.maths;
public final class FindMinRecursion {
private FindMinRecursion() {
}
/**
* Get min of an 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(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]
}
int mid = (low + high) >>> 1;
int leftMin = min(array, low, mid); // get min in [low, mid]
int rightMin = min(array, mid + 1, high); // get min in [mid+1, high]
return Math.min(leftMin, rightMin);
}
/**
* Get min of an array using recursion algorithm
*
* @param array contains elements
* @return min value of {@code array}
*/
public static int min(final int[] array) {
return min(array, 0, array.length - 1);
}
}