Created
June 25, 2012 00:52
-
-
Save rebx/2985768 to your computer and use it in GitHub Desktop.
python merge sort
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_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