1、选择排序原理
搜索整个列表,找到最小项的位置,如果该位置不是列表的第一个位置,那么就交换这两个项的位置。
2、python代码实现
def selectSort(arr): i = 0 while i < len(arr)-1: nowIndex = i j = i+1 while j < len(arr): if arr[j] < arr[i]: nowIndex = j j += 1 if nowIndex != i: temp = arr[nowIndex] arr[nowIndex] = arr[i] arr[i] = temp i += 1 return arr arr = [5,3,4,2,1] if __name__=="__main__": print(selectSort(arr))