I am writing a merge_sort program which is not running.
It shows error
if a[j] < b[k]:
TypeError: '<' not supported between instances of 'int' and 'list'
l=[int(n) for n in input().split()]
def merge_sort(l):
if len(l)==1:
return l
else:
a=l[:len(l)//2]
b=l[len(l)//2:]
print(a,b)
a=merge_sort(a)
b=merge_sort(b)
j=0
k=0
for i in range(0,len(l)):
if a[j] < b[k]:
l[i]=a[j]
j+=1
if j==len(a):
l=l.append(b[k:])
break
else:
l[i]=b[k]
k+=1
if k==len(b):
l=l.append(a[j:])
break
# print(l)
return l
print(merge_sort(l))
Here is the the error message.
Traceback (most recent call last):
File "E:/STUFF/Python/Scripts/COURSERA/merge_sort.py", line 30, in <module>
print(merge_sort(l))
File "E:/STUFF/Python/Scripts/COURSERA/merge_sort.py", line 10, in merge_sort
merge_sort(b)
File "E:/STUFF/Python/Scripts/COURSERA/merge_sort.py", line 15, in merge_sort
if a[j] < b[k]:
TypeError: '<' not supported between instances of 'int' and 'list'
Process finished with exit code 1
lin your loop. I doubt that's what you intended.lin the function is local to the function, so it's a separate name to thelin the global scope.