-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursive.java
More file actions
25 lines (23 loc) · 764 Bytes
/
Recursive.java
File metadata and controls
25 lines (23 loc) · 764 Bytes
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
public class Recursive {
public static double average(int[] a, int left, int right) {
if (left == right)
return a[left];
else {
int m = (left+right)/2;
return 1.0/(right-left+1) * ((m-left+1)*average(a, left, m) + (right-m)*average(a, m+1, right));
}
}
public static double sum(int[] a, int left, int right) {
if (left == right)
return a[left];
else {
int m = (left+right)/2;
return sum(a, left, m) + sum(a, m+1, right);
}
}
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6,7,8};
System.out.println(sum(a,0,a.length-1));
System.out.println(average(a,0,a.length-1));
}
}