Skip to content

Instantly share code, notes, and snippets.

@KerryJones
Last active July 28, 2020 04:56
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 KerryJones/c441366a88e311d33e6b to your computer and use it in GitHub Desktop.
Save KerryJones/c441366a88e311d33e6b to your computer and use it in GitHub Desktop.
Python Merge Sort
def merge_sort(array: List) -> List:
if len(array) < 2:
return array
middle = floor(len(array)/2)
left = merge_sort(array[:middle])
right = merge_sort(array[middle:])
sorted_array = []
while len(left) and len(right):
if left[0] < right[0]:
sorted_array.append(left.pop(0))
else:
sorted_array.append(right.pop(0))
return sorted_array + left + right
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment