tuoheng_algN/test/算法/插入.py

21 lines
429 B
Python

def insert(lst, index):
if lst[index-1] < lst[index]:
return
tmp = lst[index]
tmp_index = index
while tmp_index > 0 and lst[tmp_index-1] > tmp:
lst[tmp_index] = lst[tmp_index-1]
tmp_index -= 1
lst[tmp_index] = tmp
def insert_sort(lst):
for i in range(1, len(lst)):
insert(lst, i)
if __name__ == '__main__':
lst = [1, 6, 2, 7, 5]
insert_sort(lst)
print(lst)