@njit
def mergesort(arr):
 assert (arr.ndim == 1)
 if (arr.size > 2):
  mid = (arr.size // 2)
  first = mergesort(arr[:mid].copy())
  second = mergesort(arr[mid:].copy())
  left = right = 0
  writeidx = 0
  while ((left < first.size) and (right < second.size)):
   if (first[left] <= second[right]):
    arr[writeidx] = first[left]
    left += 1
   else:
    arr[writeidx] = second[right]
    right += 1
   writeidx += 1
  while (left < first.size):
   arr[writeidx] = first[left]
   writeidx += 1
   left += 1
  while (right < second.size):
   arr[writeidx] = second[right]
   writeidx += 1
   right += 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 ( arr ) : assert ( arr . ndim == 1 ) if ( arr . size > 2 ) : mid = ( arr . size // 2 ) first = mergesort ( arr [ : mid ] . copy ( ) ) second = mergesort ( arr [ mid : ] . copy ( ) ) left = right = 0 writeidx = 0 while ( ( left < first . size ) and ( right < second . size ) ) : if ( first [ left ] <= second [ right ] ) : arr [ writeidx ] = first [ left ] left += 1 else : arr [ writeidx ] = second [ right ] right += 1 writeidx += 1 while ( left < first . size ) : arr [ writeidx ] = first [ left ] writeidx += 1 left += 1 while ( right < second . size ) : arr [ writeidx ] = second [ right ] writeidx += 1 right += 1 elif ( arr . size == 2 ) : ( a , b ) = arr ( arr [ 0 ] , arr [ 1 ] ) = ( ( a , b ) if ( a <= b ) else ( b , a ) ) return arr
perform mergesort on a list of numbers .