Bubble Sort……

Bubble Sort……

while loop determines the number of cycles

list=[4165, 2315, 2165, 2123, 565, 89, 56, 56, 23, 6, 5, 1]
num=1
while num<len(list):
    for i in range(len(list)-1):
        if list[i]>list[i+1]:
            list[i] ,list[i + 1]= list[i + 1],list[i]
    num+=1
print(list)

given for loop cycles

list=[4165, 2315, 2165, 2123, 565, 89, 56, 56, 23, 6, 5, 1]
for i in range(len(list)-1):
    for i in range(len(list)-1):
        if list[i]>list[i+1]:
            list[i] ,list[i + 1]= list[i + 1],list[i]
print(list)
Published 58 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_42846555/article/details/104123251