There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
1: double findMedianSortedArrays(int A[], int m, int B[], int n) {
2: if((n+m)%2 ==0)
3: {
4: return (GetMedian(A,m,B,n, (m+n)/2) + GetMedian(A,m,B,n, (m+n)/2+1))/2.0;
5: }
6: else
7: return GetMedian(A,m,B,n, (m+n)/2+1);
8: }
9: int GetMedian(int a[], int n, int b[], int m, int k)
10: {
11: assert(a && b);
12: if (n <= 0) return b[k-1];
13: if (m <= 0) return a[k-1];
14: if (k <= 1) return min(a[0], b[0]);
15: if (b[m/2] >= a[n/2])
16: {
17: if ((n/2 + 1 + m/2) >= k)
18: return GetMedian(a, n, b, m/2, k);
19: else
20: return GetMedian(a + n/2 + 1, n - (n/2 + 1), b, m, k - (n/2 + 1));
21: }
22: else
23: {
24: if ((m/2 + 1 + n/2) >= k)
25: return GetMedian( a, n/2,b, m, k);
26: else
27: return GetMedian( a, n, b + m/2 + 1, m - (m/2 + 1),k - (m/2 + 1));
28: }
29: }
Also refer to
http://www.programcreek.com/2012/12/leetcode-median-of-two-sorted-arrays-java/
http://www.acmerblog.com/leetcode-median-of-two-sorted-arrays-5330.html
http://www.lifeincode.net/programming/leetcode-median-of-two-sorted-arrays-java/
http://www.geeksforgeeks.org/median-of-two-sorted-arrays/
Method 1 (Simply count while Merging)
Use merge procedure of merge sort. Keep track of count while comparing elements of two arrays. If count becomes n(For 2n elements), we have reached the median. Take the average of the elements at indexes n-1 and n in the merged array.
int
getMedian(
int
ar1[],
int
ar2[],
int
n)
{
int
i = 0;
int
j = 0;
int
count;
int
m1 = -1, m2 = -1;
for
(count = 0; count <= n; count++)
{
if
(i == n)
{
m1 = m2;
m2 = ar2[0];
break
;
}
else
if
(j == n)
{
m1 = m2;
m2 = ar1[0];
break
;
}
if
(ar1[i] < ar2[j])
{
m1 = m2;
m2 = ar1[i];
i++;
}
else
{
m1 = m2;
m2 = ar2[j];
j++;
}
}
return
(m1 + m2)/2;
}
Method 2 (By comparing the medians of two arrays)
This method works by first getting medians of the two sorted arrays and then comparing them.
1) Calculate the medians m1 and m2 of the input arrays ar1[]
and ar2[] respectively.
2) If m1 and m2 both are equal then we are done.
return m1 (or m2)
3) If m1 is greater than m2, then median is present in one
of the below two subarrays.
a) From first element of ar1 to m1 (ar1[0...|_n/2_|])
b) From m2 to last element of ar2 (ar2[|_n/2_|...n-1])
4) If m2 is greater than m1, then median is present in one
of the below two subarrays.
a) From m1 to last element of ar1 (ar1[|_n/2_|...n-1])
b) From first element of ar2 to m2 (ar2[0...|_n/2_|])
5) Repeat the above process until size of both the subarrays
becomes 2.
6) If size of the two arrays is 2 then use below formula to get
the median.
Median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
int
getMedian(
int
ar1[],
int
ar2[],
int
n)
{
int
m1;
int
m2;
if
(n <= 0)
return
-1;
if
(n == 1)
return
(ar1[0] + ar2[0])/2;
if
(n == 2)
return
(max(ar1[0], ar2[0]) + min(ar1[1], ar2[1])) / 2;
m1 = median(ar1, n);
m2 = median(ar2, n);
if
(m1 == m2)
return
m1;
if
(m1 < m2)
{
if
(n % 2 == 0)
return
getMedian(ar1 + n/2 - 1, ar2, n - n/2 +1);
else
return
getMedian(ar1 + n/2, ar2, n - n/2);
}
else
{
if
(n % 2 == 0)
return
getMedian(ar2 + n/2 - 1, ar1, n - n/2 + 1);
else
return
getMedian(ar2 + n/2, ar1, n - n/2);
}
}
int
median(
int
arr[],
int
n)
{
if
(n%2 == 0)
return
(arr[n/2] + arr[n/2-1])/2;
else
return
arr[n/2];
}
Method 3 (By doing binary search for the median):
It requires constant time to check if ar2[j] <= ar1[i] <= ar2[j + 1]. If ar1[i] is not the median, then depending on whether ar1[i] is greater or less than ar2[j] and ar2[j + 1], you know that ar1[i] is either greater than or less than the median. Thus you can binary search for median in O(lg n) worst-case time.
For two arrays ar1 and ar2, first do binary search in ar1[]. If you reach at the end (left or right) of the first array and don't find median, start searching in the second array ar2[].
1) Get the middle element of ar1[] using array indexes left and right.
Let index of the middle element be i.
2) Calculate the corresponding index j of ar2[]
j = n – i – 1
3) If ar1[i] >= ar2[j] and ar1[i] <= ar2[j+1] then ar1[i] and ar2[j]
are the middle elements.
return average of ar2[j] and ar1[i]
4) If ar1[i] is greater than both ar2[j] and ar2[j+1] then
do binary search in left half (i.e., arr[left ... i-1])
5) If ar1[i] is smaller than both ar2[j] and ar2[j+1] then
do binary search in right half (i.e., arr[i+1....right])
6) If you reach at any corner of ar1[] then do binary search in ar2[]
int
getMedian(
int
ar1[],
int
ar2[],
int
n)
{
return
getMedianRec(ar1, ar2, 0, n-1, n);
}
int
getMedianRec(
int
ar1[],
int
ar2[],
int
left,
int
right,
int
n)
{
int
i, j;
if
(left > right)
return
getMedianRec(ar2, ar1, 0, n-1, n);
i = (left + right)/2;
j = n - i - 1;
if
(ar1[i] > ar2[j] && (j == n-1 || ar1[i] <= ar2[j+1]))
{
if
(i == 0 || ar2[j] > ar1[i-1])
return
(ar1[i] + ar2[j])/2;
else
return
(ar1[i] + ar1[i-1])/2;
}
else
if
(ar1[i] > ar2[j] && j != n-1 && ar1[i] > ar2[j+1])
return
getMedianRec(ar1, ar2, left, i-1, n);
else
return
getMedianRec(ar1, ar2, i+1, right, n);
}
The above solutions can be optimized for the cases when all elements of one array are smaller than all elements of other array. For example, in method 3, we can change the getMedian() function to following so that these cases can be handled in O(1) time. Thanks to
nutcracker for suggesting this optimization.
int getMedian( int ar1[], int ar2[], int n)
{
if (ar1[n-1] < ar2[0])
return (ar1[n-1]+ar2[0])/2;
if (ar2[n-1] < ar1[0])
return (ar2[n-1]+ar1[0])/2;
return getMedianRec(ar1, ar2, 0, n-1, n);
}
|
X.
http://www.acmerblog.com/leetcode-median-of-two-sorted-arrays-5330.html
直接遍历,复杂度O(m+n)
01 | double findMedianSortedArrays( int A[], int m, int B[], int n) |
03 | int i=0, j=0, median = m+n; |
04 | double prev=0, last=0; |
08 | if (m == 0 && n == 0) return 0; |
09 | if (m==1) return A[0]; |
13 | while ( (i+j) <= (median/2) ) |
38 | if ((median & 1) == 0) |
39 | return (prev + last) / 2.0; |
http://segmentfault.com/a/1190000004151512
Read full article from
水中的鱼: [LeetCode] Median of Two Sorted Arrays 解题报告