手机
当前位置:查字典教程网 >脚本专栏 >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插入排序算法的实现代码

python 测试实现方法

python备份文件以及mysql数据库的脚本代码

python快速排序代码实例

python利用hook技术破解https的实例代码

用python实现批量重命名文件的代码

python 排列组合之itertools

Python中的文件和目录操作实现代码

Python常见文件操作的函数示例代码

精品推荐
分类导航