欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python冒泡排序算法的實(shí)現(xiàn)代碼

 更新時(shí)間:2013年11月21日 11:57:08   作者:  
這篇文章主要介紹了python冒泡排序算法的實(shí)現(xiàn)代碼,大家參考使用

1.算法描述:
(1)共循環(huán) n-1 次
(2)每次循環(huán)中,如果 前面的數(shù)大于后面的數(shù),就交換
(3)設(shè)置一個(gè)標(biāo)簽,如果上次沒有交換,就說(shuō)明這個(gè)是已經(jīng)好了的。

2.python冒泡排序代碼

復(fù)制代碼 代碼如下:

#!/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)



結(jié)果:[2, 3, 4, 21, 33, 44, 45, 67]

相關(guān)文章

最新評(píng)論