Created
May 27, 2013 15:57
-
-
Save edumelzer/9bb5304e7530ad426269 to your computer and use it in GitHub Desktop.
Merge Sort 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
print 'Digite os valores a serem ordenados separados por virgula:' | |
valores = [2,5,3,9,8,4] | |
def merge_sort(li): | |
if len(li) < 2: return li | |
m = len(li) / 2 | |
return merge(merge_sort(li[:m]), merge_sort(li[m:])) | |
def merge(l, r): | |
result = [] | |
i = j = 0 | |
while i < len(l) and j < len(r): | |
if l[i] < r[j]: | |
result.append(l[i]) | |
i += 1 | |
else: | |
result.append(r[j]) | |
j += 1 | |
result += l[i:] | |
result += r[j:] | |
return result | |
print merge_sort(valores) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment