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:
Lukas 2023-10-05 22:01:27 +02:00 committed by GitHub
parent 06d6e2116b
commit 064ca8f591
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 33 deletions

View File

@ -1,38 +1,13 @@
package com.thealgorithms.maths;
import java.util.Arrays;
import java.util.Random;
public final class FindMinRecursion {
public class 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();
private FindMinRecursion() {
}
/**
* Get min of 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(int[] array, int low, int high) {
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]
}
@ -52,7 +27,7 @@ public class FindMinRecursion {
* @param len length of given array
* @return min value of {@code array}
*/
public static int min(int[] array) {
return array.length == 1 ? array[0] : min(array, 0, array.length);
public static int min(final int[] array) {
return min(array, 0, array.length - 1);
}
}

View File

@ -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[] {}));
}
}