Skip to content

Instantly share code, notes, and snippets.

@rebx
Created June 25, 2012 00:52
Show Gist options
  • Save rebx/2985768 to your computer and use it in GitHub Desktop.
Save rebx/2985768 to your computer and use it in GitHub Desktop.
python merge sort
def merge_sort(list_struct=[]):
"""
merge_sort
as if there's not enough merge sorts in python out there
"""
list_len = len(list_struct)
if list_len <= 1:
return list_struct
mid = list_len / 2
left = list_struct[:mid]
right = list_struct[mid:]
left = merge_sort(left)
right = merge_sort(right)
new_list_struct = []
while left and right:
if left[0] < right[0]:
new_list_struct.append(left.pop(0))
else:
new_list_struct.append(right.pop(0))
if left:
new_list_struct.extend(left)
if right:
new_list_struct.extend(right)
return new_list_struct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment