本文摘自php中文网,作者不言,侵删。
本篇文章给大家分享了Python中的插入排序实现的代码,有感兴趣的朋友可以看一下思想:
类似于整理纸牌:摸出一张牌,插到一把牌中正确的位置(将它与手中每一张牌从右到左依次比较)
插入排序伪代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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 的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python数据竖着怎么变横的?
js和Python区别大不大
Python怎么导入模块
Python螺旋线怎么画
Python中函数定义的关键字是
Python怎么求最大公约数和最小公倍数
Python如何取set元素个数
分享Python snownlp的实例教程
Python字符串怎么实现contains功能
Python中def是什么意思
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 【插入排序实现】python