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 .
In order to solve the problem of finding median, we can consider a more general problem, i.e. finding the -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 elements of the first -th elements, and recurse the smaller parts with size. Thus we only need 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
Comparing the two element: , .
- If , it means all the elements {} are below the minimum element in array . Thus they can be deleted. Because they're irrelevant with the the minimum element. Then recurse the smaller parts with (k-pA) size.
Proof:
- We duduce it by controdiction.
- If not, at least should be the k-th minimum element in array .
- On the other hand, as , there're at most (pB-1) elements, {}, smaller than . Thus, there're at most (pA-1)+(pB-1) elements, {}, less than . That means at most be the , i.e. the (k-1)-th element in array .
- From above, we get the controdition.
- If , we can say all the elements {} are below the -th minimum element in array with the same deduction.
Thus they can be deleted. Because they're irrelevant with the the minimum element. Then recurse the smaller parts with (k-pB) size. -
If , it just means we get the minimum element, which equals to or .
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 (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; } }; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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)