forked from SheenamYadav/mca101_vidul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.py
31 lines (28 loc) · 774 Bytes
/
merge.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
'''
Objective: To merge two sorted list
return value: merged list
input parameters: lis1 and list2 two sorted lists
approach: Recursion
'''
import pdb
def merge(list1,list2,list3=[],idx1=0,idx2=0):
#pdb.set_trace()
if(len(list3)==len(list1)+len(list2)):
return list3
elif len(list1)==idx1:
list3.extend(list2[idx2:])
return list3
elif len(list2)==idx2 :
list3.extend(list1[idx1:])
return list3
elif list1[idx1]>=list2[idx2]:
list3.append(list2[idx2])
idx2=idx2+1
return merge(list1,list2,list3,idx1,idx2)
else:
list3.append(list1[idx1])
idx1=idx1+1
return merge(list1,list2,list3,idx1,idx2)
list1=[2,4,70,90]
list2=[1,3,80]
list3=merge(list2,list1)