Consider this piece of program.
def swaplist(a,i,j):
t=a[i]
a[i]=a[j]
a[j]=t
return a
def sortlist(b):
for i in range(len(b)):
for j in range(len(b)):
if b[i] > b[j]:
swaplist(b,i,j)
return b
l=[3,1,2,2,3,1,6,7,4,5,7,8,4,2,54,74,2,7,36,3]
sortlist(l)
And the output is:
That's not what I was expecting. Rather, I was expecting the sorting in ascending order. Is there something wrong? However, if you replace (len(b)) by (i,len(b)) in for j in range(len(b)) of def sortlist, the output is in ascending order.Can someone state why we get this weird output?
(Edited by Dr. Amit Kulshrestha - original submission Tuesday, August 28, 2018, 6:27 PM)