Python实现部分函数-SelectionSort(arr): 选择排序法

#选择排序法

方法一:

def selectionSort(arr):
    length = len(arr)
    for i in range(0,length-1):
        index = i
        for j in range(i+1,length):
            if arr[j]

方法二:

def FindSmallest(arr):
    smallest=arr[0]
    smallest_index=0
    for i in range(1,len(arr)):
        if arr[i] < smallest:
            smallest = arr[i]
            smallest_index = i
    return smallest_index

def SelectionSort(arr):
    new_arr=[]
    for i in range(len(arr)):
        smallest=FindSmallest(arr)
        new_arr.append(arr.pop(smallest))
    return new_arr

if __name__ == '__main__':
        arr = [5, 3, 6, 2, 10]
        print(SelectSortion(arr))

你可能感兴趣的:(Python基础学习)