-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesort.py
42 lines (40 loc) · 1.18 KB
/
mergesort.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
36
37
38
39
40
41
42
import pdb
pdb.set_trace()
def mergesort(lst1,lst2,lst3=[],idx1=0,idx2=0):
'''
objective:to merge two sorted list into third list
input parameters:
lst1,lst2:two sorted list
lst3:third list with elements of the lst1 and lst2 sorted
approach:using recursion
'''
if (len(lst3)==len(lst1)+len(lst2)):
return (lst3)
elif (len(lst1)==idx1):
lst3.extend(lst2[idx2:])
return (lst3)
elif (len(lst2)==idx2):
lst3.extend(lst1[idx1:])
return (lst3)
elif (lst1[idx1]<=lst2[idx2]):
lst3.append(lst1[idx1])
idx1=idx1+1
return mergesort(lst1,lst2,lst3,idx1,idx2)
elif(lst1[idx1]>=lst2[idx2]):
lst3.append(lst2[idx2])
idx2=idx2+1
return mergesort(lst1,lst2,lst3,idx1,idx2)
def main():
'''
objective:to merge two sorted list into third list
input parameters:
lst1,lst2:two sorted list
lst3:third list with elements of the lst1 and lst2 sorted
approach:using recursion
'''
lst1=[1,4,5]
lst2=[3,8 ,9,11]
lst3=mergesort(lst1,lst2)
print(lst3)
if __name__=='__main__':
main()