Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. testList = [12,3,82,13,9,64,27,48,87]
  2. testListB = [828,93,201,10,5,9,1,4]
  3.  
  4. def sepList(list):
  5.     newList = list[:len(list)/2]
  6.     for item in newList:
  7.         list.remove(item)
  8.     return newList
  9.  
  10. def mergeLists(listA,listB):
  11.     newList = []
  12.     for i in range(len(listA)):
  13.         if listA[i] < listB[i]:
  14.             newList.append(listA[i])
  15.             newList.append(listB[i])
  16.         else:
  17.             newList.append(listB[i])
  18.             newList.append(listA[i])
  19.     if len(listA) < len(listB):
  20.         newList.append(listB[len(listA)])
  21.     return newList
  22.  
  23. def mergeSort(list):
  24.     newList = list
  25.     if len(list) <= 1:
  26.         return newList
  27.     else:
  28.         listA = sepList(newList)
  29.         listB = newList
  30.         listA = mergeSort(listA)
  31.         listB = mergeSort(listB)
  32.         newList = mergeLists(listA,listB)
  33.     return newList
  34.  
  35. print(testList)
  36. print(mergeSort(testList))
  37. print(testListB)
  38. print(mergeSort(testListB))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement