Created
October 30, 2013 19:58
-
-
Save mccricardo/7239172 to your computer and use it in GitHub Desktop.
Merge Sort example
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(l1, l2): | |
result = [] | |
i, j = 0, 0 | |
while i < len(l1) and j < len(l2): | |
if l1[i] > l2[j]: | |
result.append(l2[j]) | |
j += 1 | |
else: | |
result.append(l1[i]) | |
i += 1 | |
result += l1[i:] | |
result += l2[j:] | |
return result | |
def merge_sort(list): | |
if len(list) <= 1: | |
return list | |
return merge(merge_sort(list[:len(list)/2]), merge_sort(list[len(list)/2:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment