用Python写冒泡排序代码
2016-04-12 10:39
916 查看
python代码实现冒泡排序代码其实很简单,具体代码如下所示:
代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 def bubbleSort(numbers): for j in xrange(len(numbers),-1,-1): for i in xrange(0,j-1,1): if numbers[i] > numbers[i+1]: numbers[i],numbers[i+1] = numbers[i+1],numbers[i] print numbers def main(): numbers = [23,12,9,15,6] bubbleSort(numbers) if __name__ == '__main__': main()
输出结果为
[12, 9, 15, 6, 23]
[9, 12, 6, 15, 23]
[9, 6, 12, 15, 23]
[6, 9, 12, 15, 23]
[6, 9, 12, 15, 23]
[6, 9, 12, 15, 23]
好了,代码到此就给大家介绍完了,希望对大家有所帮助!
您可能感兴趣的文章:
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- 在命令行用 sort 进行排序
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例