cleanup FindMinRecursion
(#4568)
* Create FindMinRecusionTest.java * Update FindMinRecursion.java * Update FindMinRecursion.java * Update FindMinRecursion.java * Rename FindMinRecusionTest.java to FindMinRecursionTest.java * Update FindMinRecursionTest.java * style: remove unused imports --------- Co-authored-by: vil02 <65706193+vil02@users.noreply.github.com>
This commit is contained in:
parent
06d6e2116b
commit
064ca8f591
@ -1,38 +1,13 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
import java.util.Arrays;
|
public final class FindMinRecursion {
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class FindMinRecursion {
|
private 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();
|
|
||||||
}
|
}
|
||||||
|
public static int min(final int[] array, final int low, final int high) {
|
||||||
/**
|
if (array.length == 0) {
|
||||||
* Get min of array using divide and conquer algorithm
|
throw new IllegalArgumentException("array must be non-empty.");
|
||||||
*
|
}
|
||||||
* @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) {
|
|
||||||
if (low == high) {
|
if (low == high) {
|
||||||
return array[low]; // or array[high]
|
return array[low]; // or array[high]
|
||||||
}
|
}
|
||||||
@ -52,7 +27,7 @@ public class FindMinRecursion {
|
|||||||
* @param len length of given array
|
* @param len length of given array
|
||||||
* @return min value of {@code array}
|
* @return min value of {@code array}
|
||||||
*/
|
*/
|
||||||
public static int min(int[] array) {
|
public static int min(final int[] array) {
|
||||||
return array.length == 1 ? array[0] : min(array, 0, array.length);
|
return min(array, 0, array.length - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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[] {}));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user