python插入排序算法实例分析


Posted in Python onJuly 03, 2015

本文实例讲述了python插入排序算法。分享给大家供大家参考。具体如下:

def insertsort(array): 
  for removed_index in range(1, len(array)): 
    removed_value = array[removed_index] 
    insert_index = removed_index 
    while insert_index > 0 and array[insert_index - 1] > removed_value: 
      array[insert_index] = array[insert_index - 1] 
      insert_index -= 1 
    array[insert_index] = removed_value

另外一个版本:

def insertsort(array): 
  for lastsortedelement in range(len(array)-1): 
    checked = lastsortedelement 
    while array[checked] > array[lastsortedelement + 1] and checked >= 0: 
      checked -= 1 
    #Insert the number into the correct position 
    array[checked+1], array[checked+2 : lastsortedelement+2] = array[lastsortedelement+1], array[checked+1 : lastsortedelement+1] 
  return array

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python常用正则表达式符号浅析
Aug 13 Python
Python的高级Git库 Gittle
Sep 22 Python
Python中函数的参数传递与可变长参数介绍
Jun 30 Python
Python将图片批量从png格式转换至WebP格式
Aug 22 Python
matplotlib中legend位置调整解析
Dec 19 Python
Python装饰器模式定义与用法分析
Aug 06 Python
Python实现Dijkstra算法
Oct 17 Python
python远程邮件控制电脑升级版
May 23 Python
python3实现单目标粒子群算法
Nov 14 Python
selenium+python配置chrome浏览器的选项的实现
Mar 18 Python
使用tensorflow根据输入更改tensor shape
Jun 23 Python
python 模拟登陆github的示例
Dec 04 Python
python列出目录下指定文件与子目录的方法
Jul 03 #Python
python清除字符串里非字母字符的方法
Jul 02 #Python
python清除字符串里非数字字符的方法
Jul 02 #Python
python实现在控制台输入密码不显示的方法
Jul 02 #Python
python获取外网ip地址的方法总结
Jul 02 #Python
python实现将英文单词表示的数字转换成阿拉伯数字的方法
Jul 02 #Python
python脚本内运行linux命令的方法
Jul 02 #Python
You might like
PHP 增加了对 .ZIP 文件的读取功能
2006/10/09 PHP
php中实现简单的ACL 完结篇
2011/09/07 PHP
PHP-redis中文文档介绍
2013/02/07 PHP
php学习笔记之基础知识
2014/11/08 PHP
php实现支持中文的文件下载功能示例
2017/08/30 PHP
Yii 使用intervention/image拓展实现图像处理功能
2019/06/22 PHP
php探针不显示内存解决方法
2019/09/17 PHP
仿新浪微博返回顶部的jquery实现代码
2012/10/01 Javascript
js里怎么取select标签里的值并修改
2012/12/10 Javascript
jquery获取对象的方法足以应付常见的各种类型的对象
2014/05/14 Javascript
js 去除字符串第一位逗号的方法
2014/06/07 Javascript
初识SmartJS - AOP三剑客
2014/06/08 Javascript
window.onerror()的用法与实例分析
2016/01/27 Javascript
vue.js+boostrap项目实践(案例详解)
2016/09/21 Javascript
js实现图片切换(动画版)
2016/12/25 Javascript
微信小程序实现下拉刷新和轮播图效果
2017/11/21 Javascript
用node-webkit把web应用打包成桌面应用(windows环境)
2018/02/01 Javascript
详解webpack4升级指南以及从webpack3.x迁移
2018/06/12 Javascript
vue根据值给予不同class的实例
2018/09/29 Javascript
VUE+Element环境搭建与安装的方法步骤
2019/01/24 Javascript
JavaScript表格隔行变色和Tab标签页特效示例【附jQuery版】
2019/07/11 jQuery
python利用有道翻译实现"语言翻译器"的功能实例
2017/11/14 Python
python对excel文档去重及求和的实例
2018/04/18 Python
Python实现查找最小的k个数示例【两种解法】
2019/01/08 Python
使用Python创建简单的HTTP服务器的方法步骤
2019/04/26 Python
python中eval与int的区别浅析
2019/08/11 Python
python实现的读取网页并分词功能示例
2019/10/29 Python
Python web框架(django,flask)实现mysql数据库读写分离的示例
2020/11/18 Python
智能电子应届生求职信
2013/11/10 职场文书
心理健康课教学反思
2014/02/13 职场文书
新闻学专业求职信
2014/07/28 职场文书
学习焦裕禄同志为人民服务思想汇报
2014/09/10 职场文书
房地产营销活动策划方案
2014/09/15 职场文书
大学生操行评语大全
2014/12/31 职场文书
2015年全国“爱牙日”宣传活动总结
2015/03/23 职场文书
2015初中政治教学工作总结
2015/07/21 职场文书