Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def merge(lst1, lst2):
- if len(lst1) == 0:
- return lst2
- if len(lst2) == 0:
- return lst1
- if lst1[0] > lst2[0]:
- return [lst2[0]] + merge(lst1, lst2[1:])
- else:
- return [lst1[0]] + merge(lst1[1:], lst2)
- def split(lst):
- if len(lst) == 0:
- return [], []
- lst1, lst2 = split(lst[2:])
- lst1.insert(0, lst[0])
- if len(lst) >= 2:
- lst2.insert(0, lst[1])
- return lst1, lst2
- def mergeSort(lst):
- if len(lst) == 1:
- return lst
- pair = split(lst)
- sortedLeft = mergeSort(pair[0])
- sortedRight = mergeSort(pair[1])
- return merge(sortedLeft, sortedRight)
- lst1 = [int(item) for item in input("Enter the list items : ").split()]
- print(mergeSort(lst1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement