插入排序的python实现

import random
import datetime
 
 
def InsertSort(data) -> list:
"""
 
:param data:
:return:
"""
length = len(data)
if length == 0:
return data
start = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(start)
for i in range(1, length):
insertValue = data[i]
insertIndex = i - 1
while insertIndex >= 0 and data[insertIndex] < insertValue:
# 将大的数往前移动 小的数往后移动
data[insertIndex + 1] = data[insertIndex]
insertIndex -= 1
data[insertIndex + 1] = insertValue
end = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(end)
return data
 
 
if __name__ == "__main__":
datas = list()
for j in range(10):
integer = random.choice(range(1, 2017))
datas.append(integer)
print("排序前 --> %s" % datas)
sort_result = InsertSort(datas)
print("排序后 -->%s" % sort_result)
posted @ 2018-01-13 15:37  晴天小猫  阅读(184)  评论(0编辑  收藏  举报