I'm working on an algorithm for partitioning arrays so that the left side has half of the elements. The thought is to perform a binary search on the partition index (i) in array (A). By calculating (j) to ensure the total left size is half, I can check conditions like (A[i-1] \leq B[j]). I need to handle edge cases by treating boundaries as plus/minus infinity. The efficiency of this algorithm is (O(\log m)) and space complexity is (O(1)). Here’s a standard O(log n) algorithm (binary search) to find the median of two sorted arrays.
Assume:
Arrays are A and B
Lengths are m = len(A) and n = len(B)
Both are sorted in non-decreasing order
We will do binary search on the smaller array to keep complexity O(log(min(m, n))).
High-level idea
We conceptually "merge" A and B without actually merging them.
The median is determined by a partition of the combined array such that:
Left side has half of the elements (or one more if total length is odd)
All elements on the left are ≤ all elements on the right
We find such a partition using binary search.
Algorithm
Ensure A is the smaller array
If m > n, swap A and B (and their lengths). So m <= n holds.
Binary search on A
We search for a partition index i in A (0 ≤ i ≤ m).
For each choice of i, the corresponding partition index in B is:
[
j = \left\lfloor\frac{m + n + 1}{2}\right\rfloor - i
]
This ensures the left part has (m+n+1)/2 elements in total.
Partition definition
Left part: A[0..i-1] and B[0..j-1]
Right part: A[i..m-1] and B[j..n-1]
Conditions for a correct partition
A correct partition must satisfy:
i == 0 or A[i-1] <= B[j]
j == 0 or B[j-1] <= A[i]
If these hold, we have found the correct partition.
Binary search steps
Initialize low = 0, high = m.
While low <= high:
i = (low + high) // 2
j = (m + n + 1) // 2 - i
Check:
If i > 0 and A[i-1] > B[j]:
We are too far right in A; move left: high = i - 1
Else if i < m and B[j-1] > A[i]:
We are too far left in A; move right: low = i + 1
Else:
We found the correct partition.
Compute the median from the partition
Once we have the correct i, j:
Let:
maxLeft be the maximum of the left partition:
If i == 0: maxLeft = B[j-1]
Else if j == 0: maxLeft = A[i-1]
Else: maxLeft = max(A[i-1], B[j-1])
Let:
minRight be the minimum of the right partition:
If i == m: minRight = B[j]
Else if j == n: minRight = A[i]
Else: minRight = min(A[i], B[j])
Then:
If (m + n) is odd: median = maxLeft
Else (even total length): median = (maxLeft + minRight) / 2.0
Pseudocode
python
def findMedianSortedArrays(A, B):
m, n = len(A), len(B)
# Ensure A is the smaller array
if m > n:
A, B, m, n = B, A, n, m
low, high = 0, m
half_len = (m + n + 1) // 2
while low <= high:
i = (low + high) // 2
j = half_len - i
# Handle edges with sentinels
A_left_max = float('-inf') if i == 0 else A[i-1]
A_right_min = float('inf') if i == m else A[i]
B_left_max = float('-inf') if j == 0 else B[j-1]
B_right_min = float('inf') if j == n else B[j]
if A_left_max > B_right_min:
# Move partition i to the left
high = i - 1
elif B_left_max > A_right_min:
# Move partition i to the right
low = i + 1
else:
# Correct partition found
maxLeft = max(A_left_max, B_left_max)
if (m + n) % 2 == 1:
return maxLeft # odd
minRight = min(A_right_min, B_right_min)
return (maxLeft + minRight) / 2.0 # even
Complexity
We do binary search on the smaller array: at most O(log(min(m, n))) steps.
Each step uses constant time operations.
So time complexity is O(log(min(m, n))) (often written as O(log n)), and space complexity is O(1).