时间:2021-07-01 10:21:17 帮助过:66人阅读
思想:
类似于整理纸牌:摸出一张牌,插到一把牌中正确的位置(将它与手中每一张牌从右到左依次比较)
插入排序伪代码:
INSERTION-SORT(A) for j <-- 2 to length[A] do key <-- A[j] i <-- j-1 while i>0 and A[i]>key do A[i+1] <-- A[i] i <-- i-1 A[i+1] <-- key python实现: def insertion_sort(A) for j in range(1 , len(A)); key = A[j] i = j - 1 while i>=0 and A[i]>key; A[i+1] = A [i] i = i - 1 A[i+1] = key A = [5,2,4,6,1,3] insertion_sort(A) print(A)
以上就是【插入排序实现】python 的详细内容,更多请关注Gxl网其它相关文章!