I have the following program in Python for bubble sort which takes as input the number of passes and a unsorted list:
def bubblesort(passnum,list=[]):
for j in range (passnum):
for i in range (len(list)):
if list[i-1]<list[i]:
continue
elif list[i-1]>list[i]:
temp=list[i]
list[i]=list[i-1]
list[i-1]=temp
list=[54,26,93,17,77,31,44,55,20]
passnum=int(input("Enter the number of passes\n"))
bubblesort(passnum,list)
print(list)
But it is sorting the list on first pass but not working on subsequent passes. Can someone tell what's wrong, since i am still a beginner in Python?
I get the output:
Enter the number of passes: 2
[54,17,77,31,44,55,20,26,93]
Which means the first pass ran correctly because of 93 at highest index, but then 77 should be before 93.