1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| ''' 选择排序算法 过于简单,以至于直接看代码吧。 ''' def Selection_Sort(A): N = len(A) for i in range(N): MinPosition = ScanForMin(A,i) Swap(A,i,MinPosition)
def ScanForMin(A,i): N = len(A) for j in range(i,N): count = 0 for k in range(i,N): if A[j] <= A[k]: count += 1 if count == N-i: print(A[j]) return j
def Swap(A,i,m): temp = A[i] A[i] = A[m] A[m] = temp
A = [1,5,6,2,4,3,8,9,7] Selection_Sort(A) print(A)
|