其他分享
首页 > 其他分享> > 选择排序

选择排序

作者:互联网

版本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)

 

标签:sort,ix,min,li,选择,print,new,排序
来源: https://www.cnblogs.com/navysummer/p/15360200.html