Last active
May 20, 2018 06:04
-
-
Save ikegami-yukino/70f18d5d88fd01bd4410b10b4122c66c 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 mergesort(l): | |
if len(l) > 1: | |
mid = len(l) // 2 | |
left = l[:mid] | |
right = l[mid:] | |
left = mergesort(left) | |
right = mergesort(right) | |
i = 0 | |
j = 0 | |
k = 0 | |
while i < len(left) and j < len(right): | |
if left[i] < right[j]: | |
l[k] = left[i] | |
i += 1 | |
else: | |
l[k] = right[j] | |
j += 1 | |
k += 1 | |
while i < len(left): | |
l[k] = left[i] | |
i += 1 | |
k += 1 | |
while j < len(right): | |
l[k] = right[j] | |
j += 1 | |
k += 1 | |
return l | |
l = [54, 26, 93, 17, 77, 31, 44, 55, 20] | |
l = mergesort(l) | |
print(l) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment