选择排序

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): 

    newArr = []
    for i in range(len(arr)):
        smallest = findSmallest(arr)  # 从数组中找出最小值
        newArr.append(arr.pop(smallest))  # 从数组中删除最小值,并将最小值加入到新数组中

    return newArr

 

posted @ 2020-03-31 16:21  1101011  阅读(88)  评论(0编辑  收藏  举报