FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Maths/FindMaxRecursion.java at master · loisoft/Java · GitHub
loisoft
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Maths
/
FindMaxRecursion.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
54 lines (44 loc) · 1.42 KB
Breadcrumbs
Java
/
Maths
/
FindMaxRecursion.java
Copy path
File metadata and controls
54 lines (44 loc) · 1.42 KB
Raw
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
package
Maths
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
Random
;
public
class
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
,
array
.
length
) ==
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
*
* @param array contains elements
* @param low the index of the first element
* @param high the index of the last element
* @return max of {@code array}
*/
public
static
int
max
(
int
[]
array
,
int
low
,
int
high
) {
if
(
low
==
high
) {
return
array
[
low
];
// or array[high]
}
int
mid
= (
low
+
high
) >>>
1
;
int
leftMax
=
max
(
array
,
low
,
mid
);
// get max in [low, mid]
int
rightMax
=
max
(
array
,
mid
+
1
,
high
);
// get max in [mid+1, high]
return
Math
.
max
(
leftMax
,
rightMax
);
}
/**
* Get max of array using recursion algorithm
*
* @param array contains elements
* @param len length of given array
* @return max value of {@code array}
*/
public
static
int
max
(
int
[]
array
,
int
len
) {
return
len
==
1
?
array
[
0
] :
Math
.
max
(
max
(
array
,
len
-
1
),
array
[
len
-
1
]);
}
}
Back
|
FazBrowse Home
|
New Git URL