-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathmerge_sort.py
35 lines (33 loc) · 978 Bytes
/
merge_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def mergeSort(nlist):
print("Splitting ",nlist)
if len(nlist)>1:
mid = len(nlist)//2
lefthalf = nlist[:mid]
righthalf = nlist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
nlist[k]=lefthalf[i]
i=i+1
else:
nlist[k]=righthalf[j]
j=j+1
k=k+1
#for remaining elements in lefthalf(if any)
while i < len(lefthalf):
nlist[k]=lefthalf[i]
i=i+1
k=k+1
##for remaining elements in righthalf(if any)
while j < len(righthalf):
nlist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",nlist)
print("Unsorted Input -- ,end='')
nlist = list(map(int, input().split()))
mergeSort(nlist)
print("Sorted Output -- , end='')
print(nlist)