Skip to content

Commit b8ec62e

Browse files
add find min
1 parent c72e31f commit b8ec62e

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.examplehub.maths;
2+
3+
import java.util.Arrays;
4+
5+
public class FindMin {
6+
7+
/**
8+
* Find min value in array.
9+
*
10+
* @param numbers the numbers to be find.
11+
* @return min value of numbers.
12+
*/
13+
public static int findMin(int[] numbers) {
14+
int min = numbers[0];
15+
for (int number : numbers) {
16+
if (number < min) {
17+
min = number;
18+
}
19+
}
20+
return min;
21+
}
22+
23+
public static int findMinSecond(int[] numbers) {
24+
return Arrays.stream(numbers).min().getAsInt();
25+
}
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.examplehub.maths;
2+
3+
public class FindMinRecursion {
4+
5+
/**
6+
* Find min value in array using recursion.
7+
*
8+
* @param numbers the numbers to find.
9+
* @param length the length of array.
10+
* @return min value in given array.
11+
*/
12+
public static int min(int[] numbers, int length) {
13+
if (length == 1) {
14+
return numbers[0];
15+
}
16+
int temp = min(numbers, length - 1);
17+
return Math.min(numbers[length - 1], temp);
18+
}
19+
20+
/**
21+
* Find min value in array using recursion.
22+
*
23+
* @param numbers the numbers to find.
24+
* @param left the left index of sub array.
25+
* @param right the right index of sub array.
26+
* @return min value in given array.
27+
*/
28+
public static int min(int[] numbers, int left, int right) {
29+
if (right == left) {
30+
return numbers[right];
31+
}
32+
int middle = (left + right) >> 1;
33+
int leftMin = min(numbers, left, middle); /* find min in range[left, middle] */
34+
int rightMin = min(numbers, middle + 1, right); /* find min in range[middle + 1, right] */
35+
return Math.min(leftMin, rightMin);
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.examplehub.maths;
2+
3+
import com.examplehub.utils.RandomUtils;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
class FindMinRecursionTest {
10+
11+
private int[] numbers;
12+
13+
@BeforeEach
14+
void setup() {
15+
numbers = RandomUtils.randomInts(-50, 50, 50);
16+
}
17+
18+
@Test
19+
void test() {
20+
int min = FindMinRecursion.min(numbers, numbers.length);
21+
for (int number : numbers) {
22+
assertTrue(min <= number);
23+
}
24+
25+
int left = 0;
26+
int right = numbers.length - 1;
27+
min = FindMinRecursion.min(numbers, left, right);
28+
for (int number : numbers) {
29+
assertTrue(min <= number);
30+
}
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.examplehub.maths;
2+
3+
import com.examplehub.utils.RandomUtils;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
class FindMinTest {
10+
private int[] numbers;
11+
12+
@BeforeEach
13+
public void setup() {
14+
numbers = RandomUtils.randomInts(-50, 50, 100);
15+
}
16+
17+
@Test
18+
void testFindMax() {
19+
int min = FindMin.findMin(numbers);
20+
for (int number : numbers) {
21+
assertTrue(min <= number);
22+
}
23+
}
24+
25+
@Test
26+
void testFindMaxSecond() {
27+
int min = FindMin.findMinSecond(numbers);
28+
for (int number : numbers) {
29+
assertTrue(min <= number);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)