Skip to content

Instantly share code, notes, and snippets.

@eric100lin
Last active July 31, 2019 08:47
Show Gist options
  • Save eric100lin/12e4e1ff9a9ca8aaf438aa20206b1bbb to your computer and use it in GitHub Desktop.
Save eric100lin/12e4e1ff9a9ca8aaf438aa20206b1bbb to your computer and use it in GitHub Desktop.
find median of two sorted arrays
# From: https://www.geeksforgeeks.org/median-two-sorted-arrays-different-sizes-ologminn-m/
# def to find median of two sorted arrays
def findMedianSortedArrays(A, len_A, B, len_B):
median = 0
index_in_A = 0
index_in_B = 0
min_index = 0
max_index = len_A
half_length = (len_A + len_B + 1) // 2
while (min_index <= max_index) :
index_in_A = (min_index + max_index) // 2
index_in_B = half_length - index_in_A
# if index_in_A = len_A, it means that
# Elements from A[] in the
# second half is an empty
# set. and if index_in_B = 0, it
# means that Elements from
# B[] in the first half is
# an empty set. so it is
# necessary to check that,
# because we compare elements
# from these two groups.
# Searching on right
if (index_in_A < len_A and index_in_B > 0 and B[index_in_B - 1] > A[index_in_A]) :
min_index = index_in_A + 1
# if index_in_A = 0, it means that
# Elements from A[] in the
# first half is an empty
# set and if index_in_B = len_B, it means
# that Elements from B[] in
# the second half is an empty
# set. so it is necessary to
# check that, because we compare
# elements from these two groups.
# searching on left
elif (index_in_A > 0 and index_in_B < len_B and B[index_in_B] < A[index_in_A - 1]) :
max_index = index_in_A - 1
# we have found the
# desired halves.
else :
# this condition happens when
# we don't have any elements
# in the first half from A[]
# so we returning the last
# element in B[] from the
# first half.
if (index_in_A == 0) :
median = B[index_in_B - 1]
# and this condition happens
# when we don't have any
# elements in the first half
# from B[] so we returning the
# last element in A[] from the
# first half.
elif (index_in_B == 0) :
median = A[index_in_A - 1]
else :
median = max(A[index_in_A - 1], B[index_in_B - 1])
break
# calculating the median.
# If number of elements
# is odd there is
# one middle element.
if ((len_A + len_B) % 2 == 1) :
return median
# Elements from A[] in the
# second half is an empty set.
if (index_in_A == len_A) :
return ((median + B[index_in_B]) / 2.0)
# Elements from B[] in the
# second half is an empty set.
if (index_in_B == len_B) :
return ((median + A[index_in_A]) / 2.0)
return ((median + min(A[index_in_A], B[index_in_B])) / 2.0)
if __name__ == "__main__":
# Driver code
A = [1, 5, 11, 17]
B = [10, 13, 14, 60]
len_A = len(A)
len_B = len(B)
# we need to define the
# smaller array as the
# first parameter to make
# sure that the time complexity
# will be O(log(min(len_A,len_B)))
if (len_A <= len_B) :
print ("The median is : {}".format(findMedianSortedArrays(A, len_A, B, len_B)))
else :
print ("The median is : {}".format(findMedianSortedArrays(B, len_B, A, len_A)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment