Created
November 28, 2020 05:15
-
-
Save christianjuth/0eaf4a2bc68c6752df4c231f80d1a9b1 to your computer and use it in GitHub Desktop.
Merge Sort in Python
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
def sort(arr): | |
length = len(arr) | |
if length >= 2: | |
splitIndex = int(length / 2) | |
left = arr[0:splitIndex] | |
right = arr[splitIndex:length] | |
left = sort(left) | |
right = sort(right) | |
output = [] | |
l = 0 | |
r = 0 | |
while l < len(left) or r < len(right): | |
if r >= len(right): | |
output.append(left[l]) | |
l += 1 | |
continue | |
if l >= len(left): | |
output.append(right[r]) | |
r += 1 | |
continue | |
if left[l] < right[r]: | |
output.append(left[l]) | |
l += 1 | |
continue | |
else: | |
output.append(right[r]) | |
r += 1 | |
continue | |
return output | |
else: | |
return arr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment