Last active
June 22, 2021 06:45
-
-
Save dialektike/f1bab15e1590ed660af28c16e651e82f to your computer and use it in GitHub Desktop.
This file contains 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
test = [8,2,4,6,9,7,10,1,5,3] | |
def merge(left,right): | |
sort = [] | |
i = 0 | |
j = 0 | |
left_size = len(left) | |
right_size = len(right) | |
while left_size > i and right_size > j: | |
if left[i] > right[j]: | |
sort.append(right[j]) | |
j += 1 | |
else: | |
sort.append(left[i]) | |
i += 1 | |
if left_size > i: | |
sort.extend(left[i:]) | |
elif right_size > j: | |
sort.extend(right[j:]) | |
return sort | |
def merge_sort(unsort): | |
size = len(unsort) | |
if size > 1: | |
mid = size // 2 | |
return merge(merge_sort(unsort[:mid]),merge_sort(unsort[mid:])) | |
else: | |
return unsort | |
if __name__ == "__main__": | |
result = merge_sort(test) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment