工作原理:
首先再未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,知道所有元素均排序完毕。
# alist = [54,26,93,17,77,31,44,55,20] def select_sort(alist): n = len(alist) for j in range(0,n-1): min_index = j for i in range(1+j, n): if alist[min_index] > alist[i]: min_index = i alist[j], alist[min_index] = alist[min_index], alist[j] if __name__ == '__main__': alist = [54,26,93,17,77,31,44,55,20] select_sort(alist) print(alist)