1. 程式人生 > >選擇排序之python實現

選擇排序之python實現

bsp index dsm col 選擇排序 pytho 一個數 pan lin

def findsmallestindex(arr):
    smallnum = arr[0]
    smallindex = 0
    # 尋找最小元素的位置
    for i in range(1,len(arr)):
        if arr[i] < smallnum:
            smallnum = arr[i]
            smallindex = i
    # 返回最小元素的位置
    return smallindex

def selectfunsearch(ql):
    # res存儲的排好序的值,也是最終的返回結果
    res = []
    
for i in range(len(ql)): smallestindex = findsmallestindex(ql) res.append(ql.pop(smallestindex)) print("it is okay") return res

選擇排序,在於每一次都將一個數篩選出來,存在另一個數組中。

選擇排序之python實現