冒泡
# 冒泡有序区,无序区 def bubble_sort(li): for i in range(len(li) - 1): #第i趟 for j in range(len(li)-i-1): #j箭头位置 if li[j] > li[j+1]: li[j+1],li[j]=li[j],li[j+1] import random li = list(range(10)) random.shuffle(li) # print(li) bubble_sort(li) print(li)
def bubble_sort_2(li): for i in range(len(li) - 1): #第i趟 exchange = False for j in range(len(li)-i-1): #j箭头位置 if li[j] > li[j+1]: li[j+1],li[j]=li[j],li[j+1] exchange =True if not exchange: break