手机
当前位置:查字典教程网 >脚本专栏 >python >python冒泡排序算法的实现代码
python冒泡排序算法的实现代码
摘要:1.算法描述:(1)共循环n-1次(2)每次循环中,如果前面的数大于后面的数,就交换(3)设置一个标签,如果上次没有交换,就说明这个是已经好...

1.算法描述:

(1)共循环 n-1 次

(2)每次循环中,如果 前面的数大于后面的数,就交换

(3)设置一个标签,如果上次没有交换,就说明这个是已经好了的。

2.python冒泡排序代码

复制代码 代码如下:

#!/usr/bin/python

# -*- coding: utf-8 -*-

def bubble(l):

flag = True

for i in range(len(l)-1, 0, -1):

if flag:

flag = False

for j in range(i):

if l[j] > l[j + 1]:

l[j], l[j+1] = l[j+1], l[j]

flag = True

else:

break

print l

li = [21,44,2,45,33,4,3,67]

bubble(li)

结果:[2, 3, 4, 21, 33, 44, 45, 67]

【python冒泡排序算法的实现代码】相关文章:

python用ConfigObj读写配置文件的实现代码

python聊天程序实例代码分享

python 查找文件夹下所有文件 实现代码

Python列表推导式的使用方法

python选择排序算法的实现代码

动态创建类实例代码

python 实现插入排序算法

python开发的小球完全弹性碰撞游戏代码

python快速排序代码实例

py2exe 编译ico图标的代码

精品推荐
分类导航