版本1
def select_sort(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 li = [1,5,3,7,9,4] print("li=",li) li_new = select_sort(li) print("sorted li=",li_new)
版本2
def select_sort(li): for i in range(len(li)): min_ix = i for j in range(i+1,len(li)): if li[j] < li[min_ix]: min_ix = j li[i], li[min_ix] = li[min_ix],li[i] li = [1,5,3,7,9,4] print("li=",li) select_sort(li) print("sorted li=",li)