Skip to content

Instantly share code, notes, and snippets.

@ehsania
Created May 28, 2013 19:05
Show Gist options
  • Save ehsania/5665253 to your computer and use it in GitHub Desktop.
Save ehsania/5665253 to your computer and use it in GitHub Desktop.
Simple implementation of merge sort in python
def merge_sort(lst):
if len(lst) <= 1 :
return lst
left = merge_sort(lst[:len(lst)/2])
right = merge_sort(lst[len(lst)/2:])
return merge(left, right)
def merge(left, right):
lst = []
while len(left) > 0 and len(right) > 0:
if left[0] >= right[0]:
lst.append(right.pop(0))
else:
lst.append(left.pop(0))
lst.extend(left)
lst.extend(right)
return lst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment