SHARE
TWEET

Untitled

a guest Dec 5th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def merge_sort(lista):
  2.     if len(lista) < 2:
  3.         return lista
  4.     m = int(len(lista)/2)
  5.     left = merge_sort(lista[:m])
  6.     right = merge_sort(lista[m:])
  7.     return merge(left,right)
  8.  
  9.  
  10. def merge(lista1, lista2):
  11.     i = 0
  12.     m = len(lista1)
  13.     r = len(lista2)
  14.     j = 0
  15.     A = []
  16.     print(lista1, lista2)
  17.     while i < m and j < r:
  18.         if lista1[i] < lista2[j]:
  19.             A.append(lista1[i])
  20.             i += 1
  21.         else:
  22.             A.append(lista2[j])
  23.             j += 1
  24.     while i < m:
  25.         A.append(lista1[i])
  26.         i += 1
  27.     while j < r:
  28.         A.append(lista2[j])
  29.         j += 1
  30.     return A
  31.  
  32. test = [5,1,3,2,4,10,3,120]
  33. merge_sort(test)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top