python实现基础排序算法之(冒泡排序)
2016-02-24 17:01
731 查看
冒泡排序算法的运作如下:
比较相邻的元素。如果第一个比第二个大,就交换他们两个。对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
针对所有的元素重复以上的步骤,除了最后一个。
持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较
# -*- coding:utf-8 -*- # __author__ = 'jakey' # 该算法复杂度为n^2 两次遍历(可设置标志进行优化) import time def algorithm(args): """ 冒泡排序 :return: """ if not args or not isinstance(args, list): return [] for i in range(len(args)-1): for j in range(1, len(args)-i): if args[j-1] > args[j]: args[j], args[j-1] = args[j-1], args[j] return args if __name__ == "__main__": un_order = [93, 1, 2, 3, 12, 34, 2, 5, 7, 8, 11, 0, 79] begin_time = time.clock() result = algorithm(un_order) end_time = time.clock() print result # [0, 1, 2, 2, 3, 5, 7, 8, 11, 12, 34, 79, 93] print end_time - begin_time # 1.4e-05
博客来源: http://blog.csdn.net/juanjuel[/code]
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- 冒泡排序
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例