Skip to content

Instantly share code, notes, and snippets.

@tuvo1106
Created July 4, 2019 16:43
Show Gist options
  • Save tuvo1106/289895cbaf833ea0a68edcc42932e4c1 to your computer and use it in GitHub Desktop.
Save tuvo1106/289895cbaf833ea0a68edcc42932e4c1 to your computer and use it in GitHub Desktop.
def merge_sort(l:list):
arr = l[::]
if len(arr) < 2:
return arr
mid = len(arr)//2;
left = arr[:mid]
right = arr[mid:]
return merge(merge_sort(left), merge_sort(right))
def merge(l, r):
arr = []
i = j = 0
while i < len(l) and j < len(r):
if l[i] <= r[j]:
arr.append(l[i])
i += 1
else:
arr.append(r[j])
j += 1
while i < len(l):
arr.append(l[i])
i += 1
while j < len(r):
arr.append(r[j])
j += 1
return arr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment