Skip to content

Instantly share code, notes, and snippets.

@edumelzer
Created May 27, 2013 15:57
Show Gist options
  • Save edumelzer/9bb5304e7530ad426269 to your computer and use it in GitHub Desktop.
Save edumelzer/9bb5304e7530ad426269 to your computer and use it in GitHub Desktop.
Merge Sort Python
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