Created
July 3, 2018 11:30
-
-
Save erika-dike/5eb10230cbf229b26a52a9d1df8ed490 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(alist): | |
if len(alist) > 1: | |
mid = len(alist) // 2 | |
lefthalf = alist[:mid] | |
righthalf = alist[mid:] | |
merge_sort(lefthalf) | |
merge_sort(righthalf) | |
i = j = k = 0 | |
while i < len(lefthalf) and j < len(righthalf): | |
if lefthalf[i] < righthalf[j]: | |
alist[k] = lefthalf[i] | |
i += 1 | |
else: | |
alist[k] = righthalf[j] | |
j += 1 | |
k += 1 | |
while i < len(lefthalf): | |
alist[k] = lefthalf[i] | |
i += 1 | |
k += 1 | |
while j < len(righthalf): | |
alist[k] = righthalf[j] | |
j += 1 | |
k += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment