Created
July 4, 2019 16:43
-
-
Save tuvo1106/289895cbaf833ea0a68edcc42932e4c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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