Give that two arrays of n integers, A and B are in sorted order.
It is an algorithm that will find the median value of A ∪ B in O( log n ) time.
# Find median of two sorted array
# time complexity
# will be O(log(min(n,m)))
def median(a, b):
lenA = len(a)
lenB = len(b)
lenT = lenA + lenB
lenS = 0
arrA = None
arrB = None
if (lenA > lenB):
lenS = lenB # binary search on small array
arrA = b # arrA is smaller
arrB = a
else:
lenS = lenA
arrA = a
arrB = b
head = 0 # head index
tail = lenS - 1 # tail index
# Using Binary Search on smaller array
def search(head,tail):
if(head == (lenS-1)): # whole array A on left
midB = int((lenB - lenA - 1) / 2)
if(lenT %2 == 0):
return (max(arrA[tail], arrB[midB]) + arrB[midB+1])/2
else:
return max(arrA[tail], arrB[midB])
if(tail < 0): # whole array A on right
midB = int((lenT-1) / 2)
if(lenT %2 == 0):
return ( arrB[midB] + min(arrA[0],arrB[midB+1]))/2
else:
return arrB[midB]
# 0 1 2 | 3 4
# int(5/2) = 2, left-hand side has 1 more ele
midA = int((tail + head)/2)
midB = int((lenT - 1)/2) - midA - 1 #
if ( arrA[midA] <= arrB[midB+1]):
if(arrB[midB] <= arrA[midA+1]):
if(lenT %2 == 0):
# Average( Max(max-left-A, max-left-B), Min(min-right-A, min-right-B) )
return (max(arrA[midA], arrB[midB]) + min(arrA[midA+1],arrB[midB+1]))/2
else:
return max(arrA[midA],arrB[midB])
else:
# move toward right on array A
newHead = midA + 1
return search(newHead, tail)
else:
# move toward left on array A
newTail = midA - 1
return search(head, newTail)
return search(head,tail)
Description:
To find the median of two arrays A, B in sorted order, the function Median sets up parameters for calling BinarySearch by passing head, tail index of array A (smaller one), two array A and B, length of them, total length, and a flag determining the merged version is in even or odd length. The BinarySearch works like the traditional binary search algorithm, searching on array A, except that we have more calculations to do in each recursive loop. Each loop will cut the size of array A in half, and doing 1 or 2 basic operations as comparison on A[midA] <= B[midB + 1] and B[midB] <= A[midA + 1], so the basic operation in each loop is just a constant c, so its recurrence relation is and T(1) = 0, which is the same as traditional binary search, so it has a complexity of θ(log n).