参考:https://blog.csdn.net/sunxianghuang/article/details/51872360
思想
每步将一个待排序的纪录,按其关键字的大小插入到已经排好序的有序数据中,直到全部插入完为止。
代码
def insert_sorted(s):
for i in range(len(s)):
temp = s[i]
if i > 0:
for j in range(i - 1, -1, -1):
if temp < s[j]:
s[j + 1] = s[j]
s[j] = temp
s1 = [12, 15, 9, 20, 6, 31, 24]
print(f'排序前:{s1}')
insert_sorted(s1)
print(f'排序后:{s1}')
结果
算法复杂度