def insert_sort(nums):
length = len(nums)
for i in range(1, length):
unsort = nums[i]
for j in range(i, -1, -1):
if unsort < nums[j - 1]:
nums[j] = nums[j - 1]
else:
break
nums[j] = unsort
return nums
if __name__ == '__main__':
nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
print(insert_sort(nums))
运行结果
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]