Created
May 28, 2013 19:05
-
-
Save ehsania/5665253 to your computer and use it in GitHub Desktop.
Simple implementation of merge sort in python
This file contains hidden or 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(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