Skip to content

Instantly share code, notes, and snippets.

@rodrickbrown
Created July 10, 2014 04:44
Show Gist options
  • Save rodrickbrown/d99c590494ddd93df821 to your computer and use it in GitHub Desktop.
Save rodrickbrown/d99c590494ddd93df821 to your computer and use it in GitHub Desktop.
import random
def merge(left, right):
result = []
i, j = 0,0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result += left[i:]
result += right[j:]
return result
def mergesort(alist):
if len(alist) < 2:
return alist
mid = int(len(alist) / 2)
left = mergesort(alist[:mid])
right = mergesort(alist[mid:])
return merge(left, right)
if __name__ == '__main__':
print(mergesort([3,9,1,2,5,0,8,7,4,6]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment