在未排序的序列中,构建子序列,直至全部排完
将待排数,插入已经排序的合适的位置
增加哨兵,放入待比较的值,和以排序的序列比较,找到合的插入点
def inster_sort(*args): lst1 = list(args) nums = [0]+lst1###增加哨兵 length = len(nums) count_swap = 0###记录交换次数 count_iter = 0###记录循环次数 for i in range(2,length): nums[0] = nums[i] j = i-1 count_iter +=1 while True: if nums[0] < nums[j]: nums[j+1] = nums[j]##大数后移 count_swap +=1 else: nums[j+1] = nums[0]##定位 break j -= 1 print(nums,count_swap,count_iter)
inster_sort(*[9,8,7,6,5,4,3,2,1])