Skip to content

Commit 064ca8f

Browse files
lukasb1bvil02
andauthored
cleanup FindMinRecursion (TheAlgorithms#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 <[email protected]>
1 parent 06d6e21 commit 064ca8f

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed
Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,13 @@
11
package com.thealgorithms.maths;
22

3-
import java.util.Arrays;
4-
import java.util.Random;
3+
public final class FindMinRecursion {
54

6-
public class FindMinRecursion {
7-
8-
/**
9-
* Driver Code
10-
*/
11-
public static void main(String[] args) {
12-
Random rand = new Random();
13-
14-
/* rand size */
15-
int size = rand.nextInt(100) + 1;
16-
int[] array = new int[size];
17-
18-
/* init array with rand numbers */
19-
for (int i = 0; i < size; i++) {
20-
array[i] = rand.nextInt() % 100;
21-
}
22-
23-
assert min(array, 0, array.length - 1) == Arrays.stream(array).min().getAsInt();
24-
assert min(array) == Arrays.stream(array).min().getAsInt();
5+
private FindMinRecursion() {
256
}
26-
27-
/**
28-
* Get min of array using divide and conquer algorithm
29-
*
30-
* @param array contains elements
31-
* @param low the index of the first element
32-
* @param high the index of the last element
33-
* @return min of {@code array}
34-
*/
35-
public static int min(int[] array, int low, int high) {
7+
public static int min(final int[] array, final int low, final int high) {
8+
if (array.length == 0) {
9+
throw new IllegalArgumentException("array must be non-empty.");
10+
}
3611
if (low == high) {
3712
return array[low]; // or array[high]
3813
}
@@ -52,7 +27,7 @@ public static int min(int[] array, int low, int high) {
5227
* @param len length of given array
5328
* @return min value of {@code array}
5429
*/
55-
public static int min(int[] array) {
56-
return array.length == 1 ? array[0] : min(array, 0, array.length);
30+
public static int min(final int[] array) {
31+
return min(array, 0, array.length - 1);
5732
}
5833
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
public class FindMinRecursionTest {
13+
14+
@ParameterizedTest
15+
@MethodSource("inputStream")
16+
void numberTests(int expected, int[] input) {
17+
Assertions.assertEquals(expected, FindMinRecursion.min(input));
18+
}
19+
20+
private static Stream<Arguments> inputStream() {
21+
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}));
22+
}
23+
24+
@Test
25+
public void testFindMaxThrowsExceptionForEmptyInput() {
26+
assertThrows(IllegalArgumentException.class, () -> FindMinRecursion.min(new int[] {}));
27+
}
28+
}

0 commit comments

Comments
 (0)