Skip to content

Instantly share code, notes, and snippets.

@horiajurcut
Created October 12, 2013 17:58
Show Gist options
  • Save horiajurcut/6952893 to your computer and use it in GitHub Desktop.
Save horiajurcut/6952893 to your computer and use it in GitHub Desktop.
def merge_sort(seq):
mid = len(seq) // 2
left, right = seq[:mid], seq[mid:]
if len(left) > 1:
left = merge_sort(left)
if len(right) > 1:
right = merge_sort(right)
res = []
while left and right:
if left[-1] >= right[-1]:
res.append(left.pop)
else:
res.append(right.pop)
res.reverse()
return (left or right) + res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment