Advertisement
JAS_Software

Merge Sort

Apr 26th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. def mergeLists(lista,listb):
  2.     listc = []
  3.     #loop through lists and add smalledt element to sorted list until one is empty
  4.     while lista != [] and listb != []:
  5.         if lista[0] <= listb[0]:
  6.             listc.append(lista.pop(0))
  7.         else:
  8.             listc.append(listb.pop(0))
  9.     if len(lista) >= 1:
  10.         listc += lista
  11.     else:
  12.         listc += listb
  13.     return listc
  14.  
  15. def mergeSort(listc):
  16.     if len(listc) <= 1:
  17.         return listc
  18.     else:
  19.         middle = int(len(listc)/2)
  20.         lista = mergeSort(listc[:middle])
  21.         listb = mergeSort(listc[middle:])
  22.         print(middle,lista,listb)
  23.     return mergeLists(lista,listb)
  24.  
  25.  
  26. lista = ['E','I','J','H','G','F','D','B','C','A']
  27. print(mergeSort(lista))
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement