Advertisement
nathanwailes

Merge sort

May 18th, 2024
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. def merge_sort(arr):
  2.     if len(arr) > 1:
  3.         mid = len(arr) // 2
  4.         L = arr[:mid]
  5.         R = arr[mid:]
  6.  
  7.         merge_sort(L)
  8.         merge_sort(R)
  9.  
  10.         i = j = k = 0
  11.         while i < len(L) and j < len(R):
  12.             if L[i] < R[j]:
  13.                 arr[k] = L[i]
  14.                 i += 1
  15.             else:
  16.                 arr[k] = R[j]
  17.                 j += 1
  18.             k += 1
  19.  
  20.         while i < len(L):
  21.             arr[k] = L[i]
  22.             i += 1
  23.             k += 1
  24.  
  25.         while j < len(R):
  26.             arr[k] = R[j]
  27.             j += 1
  28.             k += 1
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement