-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmergeTwoSortedListRecurssion.py
More file actions
31 lines (26 loc) · 1.09 KB
/
mergeTwoSortedListRecurssion.py
File metadata and controls
31 lines (26 loc) · 1.09 KB
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
def mergeSortedLists(list1, list2, mergedList=[], index1=0, index2=0):
'''
Objective: To merge to sorted lists
Input Parameters:
list1: First sorted list
list2: Second sorted list
mergedList: Merged list
index1: Index to traverse list1
index2: Index to traverse list2
Return: None
'''
#approach: Using recurssion
if (index1==(len(list1)) and index2==(len(list2))):
return mergedList
elif ((index1!=len(list1) and index2==(len(list2))) or (list1[index1] <= list2[index2])):
mergedList.append(list1[index1])
return mergeSortedLists(list1,list2,mergedList,index1+1,index2)
elif ((index1==(len(list1)) and index2!=len(list2)) or (list1[index1] > list2[index2])):
mergedList.append(list2[index2])
return mergeSortedLists(list1,list2,mergedList,index1,index2+1)
def main():
list1=[int(x) for x in input("Enter sorted list1: ").split()]
list2=[int(x) for x in input("Enter sorted list2: ").split()]
print("Merged sorted list: ",mergeSortedLists(list1,list2))
if __name__=="__main__":
main()