import random
import datetime
# 选择排序
def getMaxIndex(index, ls) -> int:
"""
:type ls list
:type index: int
:return:
"""
# 选择排序时 一开始就认为当前的数的最大的
maxIndex = index
length = len(ls)
for i in range(index, length):
if ls[i] > ls[maxIndex]:
maxIndex = i
return maxIndex
def SelectSort(data) -> list:
"""
选择排序
:type data: list
:return:
"""
length = len(data)
print("排序前-->%s" % data)
start = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(start)
for i in range(length):
theMax = getMaxIndex(i, data)
if i != theMax:
temp = data[theMax]
# 将大的数据往前挪一位
data[theMax] = data[i]
data[i] = temp
end = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(end)
return data
if __name__ == "__main__":
datas = list()
for j in range(10000):
integer = random.choice(range(1, 2017))
datas.append(integer)
sort_result = SelectSort(datas)
print("排序后 -->%s" % sort_result)
排序一万个元素大概需要5s