Created
January 31, 2013 19:17
-
-
Save liyanage/4685555 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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