冒泡,选择,插入排序法(low)
冒泡排序
def bubble_sort(li): for i in range(len(li)-1): #第i趟 exchange = False for j in range(len(li)-i-1): if li[j] > li[j+1]: li[j], li[j+1] = li[j+1], li[j] exchange = True if not exchange: return
选择排序
def select_sort_simple(li): li_new = [] for i in range(len(li)): min_val = min(li) li_new.append(min_val) li.remove(min_val) return li_new def select_sort(li): for i in range(len(li)-1): # i是第几趟 min_loc = i for j in range(i+1, len(li)): if li[j] < li[min_loc]: min_loc = j li[i], li[min_loc] = li[min_loc], li[i] print(li)
插入排序
def insert_sort(li): for i in range(1, len(li)): #i 表示摸到的牌的下标 tmp = li[i] j = i - 1 #j指的是手里的牌的下标 while j >= 0 and li[j] > tmp: li[j+1] = li[j] j -= 1 li[j+1] = tmp print(li)