@njit
def mergesort_inplace(arr):
 assert (arr.ndim == 1)
 if (arr.size > 2):
  mid = (arr.size // 2)
  first = arr[:mid]
  second = arr[mid:]
  mergesort_inplace(first)
  mergesort_inplace(second)
  left = 0
  right = mid
  while ((left < mid) and (right < arr.size)):
   if (arr[left] <= arr[right]):
    left += 1
   else:
    temp = arr[right]
    right += 1
    for i in range(mid, left, (-1)):
     arr[i] = arr[(i - 1)]
    arr[left] = temp
    left += 1
    mid += 1
 elif (arr.size == 2):
  (a, b) = arr
  (arr[0], arr[1]) = ((a, b) if (a <= b) else (b, a))
 return arr

@ njit def mergesort_inplace ( arr ) : assert ( arr . ndim == 1 ) if ( arr . size > 2 ) : mid = ( arr . size // 2 ) first = arr [ : mid ] second = arr [ mid : ] mergesort_inplace ( first ) mergesort_inplace ( second ) left = 0 right = mid while ( ( left < mid ) and ( right < arr . size ) ) : if ( arr [ left ] <= arr [ right ] ) : left += 1 else : temp = arr [ right ] right += 1 for i in range ( mid , left , ( - 1 ) ) : arr [ i ] = arr [ ( i - 1 ) ] arr [ left ] = temp left += 1 mid += 1 elif ( arr . size == 2 ) : ( a , b ) = arr ( arr [ 0 ] , arr [ 1 ] ) = ( ( a , b ) if ( a <= b ) else ( b , a ) ) return arr
inplace mergesort .