Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
58 lines (47 loc) · 1.58 KB

File metadata and controls

58 lines (47 loc) · 1.58 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package Maths;
import java.util.Arrays;
import java.util.Random;
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, array.length) == Arrays.stream(array).min().getAsInt();
}
/**
* 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) {
if (low == high) {
return array[low]; //or array[high]
}
int mid = (low + high) >>> 1;
int leftMin = min(array, low, mid); //get min in [low, mid]
int rightMin = min(array, mid + 1, high); //get min in [mid+1, high]
return Math.min(leftMin, rightMin);
}
/**
* Get min of array using recursion algorithm
*
* @param array contains elements
* @param len length of given array
* @return min value of {@code array}
*/
public static int min(int[] array, int len) {
return len == 1 ? array[0] : Math.min(min(array, len - 1), array[len - 1]);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.