Median of Two Sorted Arrays

A fast method to determine the number is odd or even:

1
2
total & 0x1 //true, if total is odd
total & 0x1 //false, if total is even

  


 

Problem Statement

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)).

In order to solve the problem of finding median, we can consider a more general problem, i.e. finding the k-th minimum element of an array. If we solve the general problem, we can get the median by

1
2
3
4
5
int total = m + n;
if(total & 0x1)
    return findKthSortedArrays(A, m, B, n, total/2+1);
else
    return (0.0 + findKthSortedArrays(A, m, B, n, total/2) + findKthSortedArrays(A, m, B, n, total/2+1)) / 2;  //use 0.0 + ... to avoid round down.

 

We will use the thinking of binary search to delete k2 elements of the first k-th elements, and recurse the smaller parts with k2 size. Thus we only need O(logk) running time for searching.

First, let's do some pre-operation. When we get two arrays A and B, we can assume that the length of A, m, is not greater than the length of B, n. Otherwise, we can swap A and B. The details as follows:

1
2
3
4
5
int double findKthSortedArrays(int A[], int m, int B[], int n, int k){
    if(m > n)
        return findKthSortedArrays(B, n, A, m, k);
    ......
}

Then we can consider the part deleted. We define

pA=min(k2,m)

pB=kpA

Comparing the two element: A[pA1], B[pB1].

  • If A[pA1]<B[pB1], it means all the elements {A[0],...,A[pA1]} are below the kth minimum element in array AB. Thus they can be deleted. Because they're irrelevant with the the kth minimum element. Then recurse the smaller parts with (k-pA) size.

Proof:

  • We duduce it by controdiction.

 

  • If not, A[pA1] at least should be the k-th minimum element in array AB.

 

  • On the other hand, as A[pA1]<B[pB1], there're at most (pB-1) elements, {B[0],...,B[pB2]}, smaller than A[pA1]. Thus, there're at most (pA-1)+(pB-1) elements, {A[0],...,A[pA2],B[0],...,B[pB2]}, less than A[pA1]. That means A[pA1] at most be the 1+(pA1)+(pB1)=pA+pB2=k1, i.e. the (k-1)-th element in array AB.

 

  • From above, we get the controdition.
  •  If B[pB1]<A[pA1], we can say all the elements {B[0],...,B[pB1]} are below the k-th minimum element in array AB with the same deduction.
    Thus they can be deleted. Because they're irrelevant with the the kth minimum element. Then recurse the smaller parts with (k-pB) size.
  • If B[pB1]==A[pA1], it just means we get the kth minimum element, which equals to B[pB1] or A[pA1].

     

We conclude the above as code:

1
2
3
4
5
6
if(A[pA-1] < B[pB-1])
    return findKthSortedArrays(A+pA, m-pA, B, n, k-pA);
else if(A[pA-1] > B[pB-1])
    return findKthSortedArrays(A, m, B+pB, n-pB, k-pB);
else
    return A[pA-1];

 

Also we need to consider the boundary condition to stop the recursion or exclude other exceptional situations.

  • when k > m + n, throw exception, i.e. assert(k <= m+n);

 

  • when m == 0, return B[k-1]; because we have assume n is bigger than m.

 

  • when k == 1, retur min(A[0], B[0]); the above boundary condition have excluded the situation A == NULL or B == NULL.

 Thus our code could be

1
2
3
4
5
assert(k <= m+n);
if(0 == m)
    return B[k-1];
if(1 == k)
    return min(A[0], B[0]);

 


 

The complete code is:

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
class Solution {
private:
    int min(int a, int b){
        return a < b ? a : b;
    }
    int findKthSortedArrays(int A[], int m, int B[], int n, int k){
        assert(k <= m+n);
        if(m > n)
            return findKthSortedArrays(B, n, A, m, k);
        if(0 == m)
            return B[k-1];
        if(1 == k)
            return min(A[0], B[0]);
             
        int pA = min(k/2, m);
        int pB = k - pA;
         
        if(A[pA-1] < B[pB-1])
            return findKthSortedArrays(A+pA, m-pA, B, n, k-pA);
        else if(A[pA-1] > B[pB-1])
            return findKthSortedArrays(A, m, B+pB, n-pB, k-pB);
        else
            return A[pA-1];
    }
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n) {
        int total = m + n;
         
        if(total & 0x1)
            return findKthSortedArrays(A, m, B, n, total/2+1);
        else
            return (0.0 + findKthSortedArrays(A, m, B, n, total/2)+findKthSortedArrays(A, m, B, n, total/2+1))/2;
    }
};

 

posted @   kid551  阅读(257)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示