Bubble sort --Python achieve

Bubble Sort is an exchange sort.

Exchange Sort: pairwise comparisons to be sort of keywords and the order does not meet the requirements that the number of exchange until the entire table to meet the requirements of the order date.

from random import shuffle
lst = [i for i in range(10)]
shuffle(lst)
flag = False
for i in range(len(lst)):
    for j in range(len(lst)-1-i):
        flag = False
        if lst[j] > lst[j+1]:
            lst[j], lst[j+1] = lst[j+1], lst[j]
            flag = True
    if not flag:
        break

print(lst)

Guess you like

Origin www.cnblogs.com/luckyleaf/p/11531155.html