I want to sort two already sorted arrays into one. I am using merge sort for this purpose.
Error:: IndexError: list index out of range
I have tried checking out this manually and I couldnt find out of range arrays. Please correct me if I am wrong
def merging(list1, list2):
m = len(list1)
n = len(list2)
val = m+n
j, k = 0, 0
new =[]
for i in range(val):
if j<m and k<n:
if list1[j] < list2[k]:
new.append(list1[j])
j += 1
else:
new.append(list2[k])
k += 1
elif j==m:
while i<m+n:
new.append(list2[k])
k += 1
i += 1
else:
while i<m+n:
new.append(list1[j])
j += 1
i += 1
print 'sorted array is:', new
if __name__ == '__main__':
print 'Enter list 1'
l1 = raw_input()
list1 = map(int, l1.split())
print 'Enter list 2'
l2 = raw_input()
list2 = map(int, l2.split())
merging(list1,list2)
EDIT:: I do not want to use any built in functions like sort()
l_new = list1 + list2; l_new.sort()