Skip to content

Instantly share code, notes, and snippets.

@mccricardo
Created October 30, 2013 19:58
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 mccricardo/7239172 to your computer and use it in GitHub Desktop.
Save mccricardo/7239172 to your computer and use it in GitHub Desktop.
Merge Sort example
def merge(l1, l2):
result = []
i, j = 0, 0
while i < len(l1) and j < len(l2):
if l1[i] > l2[j]:
result.append(l2[j])
j += 1
else:
result.append(l1[i])
i += 1
result += l1[i:]
result += l2[j:]
return result
def merge_sort(list):
if len(list) <= 1:
return list
return merge(merge_sort(list[:len(list)/2]), merge_sort(list[len(list)/2:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment