Skip to content

Instantly share code, notes, and snippets.

@christianjuth
Created November 28, 2020 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianjuth/0eaf4a2bc68c6752df4c231f80d1a9b1 to your computer and use it in GitHub Desktop.
Save christianjuth/0eaf4a2bc68c6752df4c231f80d1a9b1 to your computer and use it in GitHub Desktop.
Merge Sort in Python
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