02 選擇排序

'''選擇排序, 找到最大或最小的,放一邊,而後繼續找'''

import time
start = time.time()
def selectionSort(alist):
	for i in range(len(alist)-1):
		min_index = i
		for j in range(i+1,len(alist)):
			if alist[min_index]> alist[j]:
				min_index = j
		# 先調換index,而後在循環外面調換具體數值
		alist[i], alist[min_index] = alist[min_index], alist[i]


if __name__ =='__main__':
	alist =[10,9,8,7,6,5,4,3,2,1]
	selectionSort(alist)
	print(alist)
	end = time.time()
	print(end-start)
相關文章
相關標籤/搜索