Median of two sorted arrays with different sizes in O ( log ( min ( n , m ) ) ) | Python code for median with case of returning double value when even number of elements are present in both array combinely ; def to find max ; def to find minimum ; def to find median of two sorted arrays ; if i = n , it means that Elements from a [ ] in the second half is an empty set . and if j = 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 i = 0 , it means that Elements from a [ ] in the first half is an empty set and if j = m , 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 ; we have found the desired halves . ; 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. ; 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. ; calculating the median . If number of elements is odd there is one middle element . ; Elements from a [ ] in the second half is an empty set . ; Elements from b [ ] in the second half is an empty set . ; Driver code ; we need to define the smaller array as the first parameter to make sure that the time complexity will be O ( log ( min ( n , m ) ) )
median = 0 NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE def maximum ( a , b ) : NEW_LINE INDENT return a if a > b else b NEW_LINE DEDENT def minimum ( a , b ) : NEW_LINE INDENT return a if a < b else b NEW_LINE DEDENT def findMedianSortedArrays ( a , n , b , m ) : NEW_LINE INDENT global median , i , j NEW_LINE min_index = 0 NEW_LINE max_index = n NEW_LINE while ( min_index <= max_index ) : NEW_LINE INDENT i = int ( ( min_index + max_index ) / 2 ) NEW_LINE j = int ( ( ( n + m + 1 ) / 2 ) - i ) NEW_LINE if ( i < n and j > 0 and b [ j - 1 ] > a [ i ] ) : NEW_LINE INDENT min_index = i + 1 NEW_LINE DEDENT elif ( i > 0 and j < m and b [ j ] < a [ i - 1 ] ) : NEW_LINE INDENT max_index = i - 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT median = b [ j - 1 ] NEW_LINE DEDENT elif ( j == 0 ) : NEW_LINE INDENT median = a [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT median = maximum ( a [ i - 1 ] , b [ j - 1 ] ) NEW_LINE DEDENT break NEW_LINE DEDENT DEDENT if ( ( n + m ) % 2 == 1 ) : NEW_LINE INDENT return median NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT return ( ( median + b [ j ] ) / 2.0 ) NEW_LINE DEDENT if ( j == m ) : NEW_LINE INDENT return ( ( median + a [ i ] ) / 2.0 ) NEW_LINE DEDENT return ( ( median + minimum ( a [ i ] , b [ j ] ) ) / 2.0 ) NEW_LINE DEDENT a = [ 900 ] NEW_LINE b = [ 10 , 13 , 14 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE if ( n < m ) : NEW_LINE INDENT print ( " The ▁ median ▁ is ▁ : ▁ { } " . format ( findMedianSortedArrays ( a , n , b , m ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT echo ( " The ▁ median ▁ is ▁ : ▁ { } " . format ( findMedianSortedArrays ( b , m , a , n ) ) ) NEW_LINE DEDENT