Add FindMaxRecursionTest (#4431)
* Update FindMaxRecursion.java * Create FindMaxRecusionTest.java * Update and rename FindMaxRecusionTest.java to FindMaxRecursionTest.java * Update FindMaxRecursion.java * Update FindMaxRecursion.java * Update src/test/java/com/thealgorithms/maths/FindMaxRecursionTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/test/java/com/thealgorithms/maths/FindMaxRecursionTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/main/java/com/thealgorithms/maths/FindMaxRecursion.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update FindMaxRecursion.java * Update FindMaxRecursionTest.java * Update FindMaxRecursionTest.java * Update FindMaxRecursion.java * Update src/main/java/com/thealgorithms/maths/FindMaxRecursion.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update FindMaxRecursion.java * Update FindMaxRecursion.java * Update FindMaxRecursion.java * Update FindMaxRecursion.java * Update FindMaxRecursion.java * Update src/main/java/com/thealgorithms/maths/FindMaxRecursion.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
parent
f72b80b116
commit
8dc5505323
@ -1,26 +1,9 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
import java.util.Arrays;
|
public final class FindMaxRecursion {
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class FindMaxRecursion {
|
private FindMaxRecursion() {
|
||||||
|
|
||||||
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 max(array) == Arrays.stream(array).max().getAsInt();
|
|
||||||
assert max(array, 0, array.length - 1) == Arrays.stream(array).max().getAsInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get max of array using divide and conquer algorithm
|
* Get max of array using divide and conquer algorithm
|
||||||
*
|
*
|
||||||
@ -29,7 +12,10 @@ public class FindMaxRecursion {
|
|||||||
* @param high the index of the last element
|
* @param high the index of the last element
|
||||||
* @return max of {@code array}
|
* @return max of {@code array}
|
||||||
*/
|
*/
|
||||||
public static int max(int[] array, int low, int high) {
|
public static int max(final int[] array, final int low, final int high) {
|
||||||
|
if (array.length == 0) {
|
||||||
|
throw new IllegalArgumentException("array must be non-empty.");
|
||||||
|
}
|
||||||
if (low == high) {
|
if (low == high) {
|
||||||
return array[low]; // or array[high]
|
return array[low]; // or array[high]
|
||||||
}
|
}
|
||||||
@ -48,7 +34,7 @@ public class FindMaxRecursion {
|
|||||||
* @param array contains elements
|
* @param array contains elements
|
||||||
* @return max value of {@code array}
|
* @return max value of {@code array}
|
||||||
*/
|
*/
|
||||||
public static int max(int[] array) {
|
public static int max(final int[] array) {
|
||||||
return array.length == 1 ? array[0] : max(array, 0, array.length);
|
return max(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 FindMaxRecursionTest {
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("inputStream")
|
||||||
|
void numberTests(int expected, int[] input) {
|
||||||
|
Assertions.assertEquals(expected, FindMaxRecursion.max(input));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> inputStream() {
|
||||||
|
return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(9, new int[] {3, -2, 3, 9, -4, -4, 8}), Arguments.of(3, new int[] {3}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindMaxThrowsExceptionForEmptyInput() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> FindMaxRecursion.max(new int[] {}));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user