Created
March 23, 2012 15:25
-
-
Save yesidays/2171745 to your computer and use it in GitHub Desktop.
MergeSort #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
import random | |
import math | |
def merge(left,right): | |
result = [] | |
while left and right: | |
if left[0] < right[0]: | |
result.append(left.pop(0)) | |
else: | |
result.append(right.pop(0)) | |
if left: | |
result.extend(left) | |
else: | |
result.extend(right) | |
return result | |
def mergesort(m): | |
left = [] | |
right = [] | |
result = [] | |
if len(m) <=1: | |
return m | |
middle = len(m)/2 | |
left = m[:middle] | |
right = m[middle:] | |
left = mergesort(left) | |
right = mergesort(right) | |
if left[-1] > right[0]: | |
result = merge(left, right) | |
else: | |
result = left + right | |
return result | |
p = random.sample(range(10),8) | |
print p | |
print mergesort(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment