Created
December 4, 2013 10:37
-
-
Save shoya140/7785544 to your computer and use it in GitHub Desktop.
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(left, right): | |
| res = [] | |
| while left != [] and right != []: | |
| if left[0] < right[0]: | |
| res.append(left.pop(0)) | |
| else: | |
| res.append(right.pop(0)) | |
| res.extend(left) | |
| res.extend(right) | |
| return res | |
| def mergesort(list): | |
| if len(list) == 1: | |
| return list | |
| left = mergesort(list[0:len(list)/2]) | |
| right = mergesort(list[len(list)/2:len(list)]) | |
| return merge(left, right) | |
| if __name__ == "__main__": | |
| list = [2, 5, 3, 6, 1, 8, 9] | |
| print mergesort(list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment