Skip to content

Instantly share code, notes, and snippets.

@liyanage
Created January 31, 2013 19:17
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 liyanage/4685555 to your computer and use it in GitHub Desktop.
Save liyanage/4685555 to your computer and use it in GitHub Desktop.
def merge(left, right):
nl = len(left)
nr = len(right)
merged = []
il = 0
ir = 0
while il < nl and ir < nr:
l = left[il]
r = right[ir]
if l < r:
il += 1
merged.append(l)
else:
ir += 1
merged.append(r)
return merged + left[il:] + right[ir:]
def mergesort(input):
n = len(input)
if n < 2:
return input
return merge(mergesort(input[0:n/2]), mergesort(input[n/2:]))
mergesort([3, 2, 800, 5, 0, -5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment