選擇排序

選擇排序

算法步驟

  1. 找到序列中的最小數,放到序列的起始位置
  2. 再從剩餘的序列中繼續尋找最小的數
  3. 重複以上步驟,你將得到按衝小到大排序的序列

圖解算法

選擇排序

代碼實現

def se_sort(ls):
    for i in range(len(ls)):
        for j in range(len(ls)):
            if ls[i]<ls[j]:
                ls[j],ls[i]=ls[i],ls[j]
            else:
                pass
    return ls

ls=[34,32,3,56,78,45,1,0,56,11]
ls_result=se_sort(ls)
print(ls_result)`
#[0, 1, 3, 11, 32, 34, 45, 56, 56, 78]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章