最优时间复杂度:O(n)
最坏时间复杂度:O(n^2)
稳定性:稳定
插入排序python代码实现:
def insert_sort(list):
"""插入排序"""
n = len(list)
for i in range(1, n):
index = i
while index > 0:
if list[index] < list[index-1]:
list[index], list[index-1] = list[index-1], list[index]
index -= 1
else:
break
if __name__ == '__main__':
li = [1, 9, 8, 2, 3]
print(li)
insert_sort(li)
print(li)