Python-排序
来源:互联网 发布:造价数据库 编辑:程序博客网 时间:2024/06/05 00:19
选择排序:
def selectSort(l): size = len(l) for i in range(0,size): max = l[i] index = i for j in range(i,size): if l[j] > max: max = l[j] index = j temp = l[i] l[i] = max l[index] = temp print(l)执行结果:
>>> s = [5,8,55,3,9,3,6,5,2]>>> selectSort(s)[55, 8, 5, 3, 9, 3, 6, 5, 2][55, 9, 5, 3, 8, 3, 6, 5, 2][55, 9, 8, 3, 5, 3, 6, 5, 2][55, 9, 8, 6, 5, 3, 3, 5, 2][55, 9, 8, 6, 5, 3, 3, 5, 2][55, 9, 8, 6, 5, 5, 3, 3, 2][55, 9, 8, 6, 5, 5, 3, 3, 2][55, 9, 8, 6, 5, 5, 3, 3, 2][55, 9, 8, 6, 5, 5, 3, 3, 2]
插入排序:
def insertSort(l): size = len(l) for i in range(1,size): fv = l[i] j = i while j >= 1: if fv < l[j-1]: l[j] = l[j-1] else: break j = j - 1 l[j] = fv print(l)例子:
>>> l = [5,8,55,3,9,3,6,5,2]>>> insertSort(l)[5, 8, 55, 3, 9, 3, 6, 5, 2][5, 8, 55, 3, 9, 3, 6, 5, 2][3, 5, 8, 55, 9, 3, 6, 5, 2][3, 5, 8, 9, 55, 3, 6, 5, 2][3, 3, 5, 8, 9, 55, 6, 5, 2][3, 3, 5, 6, 8, 9, 55, 5, 2][3, 3, 5, 5, 6, 8, 9, 55, 2][2, 3, 3, 5, 5, 6, 8, 9, 55]
0 0
- python 排序
- python 排序
- Python排序
- python 排序
- Python 排序
- 排序Python
- Python 排序
- python 排序
- python 排序
- python 排序
- python 排序
- Python 排序
- Python-排序
- python 排序
- Python 排序
- python排序
- python 排序
- Python排序
- iOS报错:does not contain bitcode Xcode7
- JAVA继承引入
- jquery 弹出框插件 dialog使用介绍
- 衣着
- 优化UITableViewCell高度计算的那些事
- Python-排序
- Android开发者网址导航
- 基础概念之均方根值RMS
- Atlassian最新产品——JIRA Core,JIRA Softerware,JIRA Service Desk简介
- SCOPE 中 SPFILE、MEMORY、BOTH 的区别
- java实现base64字符串转换png图片
- 选择排序(JAVA实现)
- java并发编程的转载博客专题
- 栈--二进制转化