fork download
  1. def merge(left, m, right, vec):
  2.  
  3. i = left
  4. j = m + 1
  5. aux = []
  6.  
  7. while i <= m and j <= right:
  8.  
  9. if vec[i] < vec[j]:
  10. aux.append(vec[i])
  11. i += 1
  12. else:
  13. aux.append(vec[j])
  14. j += 1
  15.  
  16. while i <= m:
  17. aux.append(vec[i])
  18. i += 1
  19. while j <= right:
  20. aux.append(vec[j])
  21. j += 1
  22. k = 0
  23. for i in range(left, right+1):
  24. vec[i] = aux[k];
  25. k += 1
  26.  
  27. def divideEtImpera(left, right, vec):
  28.  
  29. if left < right:
  30. m = (left + right) >> 1
  31. divideEtImpera(left, m, vec)
  32. divideEtImpera(m + 1, right, vec)
  33. merge(left, m, right, vec)
  34.  
  35. def mergesort(vec):
  36. divideEtImpera(0, len(vec) - 1, vec)
  37.  
  38. def main():
  39. vec = [9,8,7,6,5,4,3,2,1,0,-1]
  40.  
  41. print(vec)
  42.  
  43. mergesort(vec)
  44.  
  45. print(vec)
  46.  
  47. main()
  48.  
Success #stdin #stdout 0.02s 9124KB
stdin
Standard input is empty
stdout
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1]
[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]