-
Notifications
You must be signed in to change notification settings - Fork 0
/
Median_sorted.java
36 lines (31 loc) · 1.28 KB
/
Median_sorted.java
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
public class Median_sorted {
public static double median(int[] a, int[] b) {
int n1 = a.length, n2 = b.length;
//if n1 is bigger swap the arrays:
if (n1 > n2) return median(b, a);
int n = n1 + n2; //total length
int left = (n1 + n2 + 1) / 2; //length of left half
//apply binary search:
int low = 0, high = n1;
while (low <= high) {
int mid1 = (low + high) / 2;
int mid2 = left - mid1;
//calculate l1, l2, r1 and r2;
int l1 = (mid1 > 0) ? a[mid1 - 1] : Integer.MIN_VALUE;
int l2 = (mid2 > 0) ? b[mid2 - 1] : Integer.MIN_VALUE;
int r1 = (mid1 < n1) ? a[mid1] : Integer.MAX_VALUE;
int r2 = (mid2 < n2) ? b[mid2] : Integer.MAX_VALUE;
if (l1 <= r2 && l2 <= r1) {
if (n % 2 == 1) return Math.max(l1, l2);
else return ((double) (Math.max(l1, l2) + Math.min(r1, r2))) / 2.0;
} else if (l1 > r2) high = mid1 - 1;
else low = mid1 + 1;
}
return 0; //dummy statement
}
public static void main(String[] args) {
int[] a = {1, 4, 7, 10, 12};
int[] b = {2, 3, 6, 15};
System.out.println("The median of two sorted arrays is " + median(a, b));
}
}