PYTHON 冒泡排序

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def bubble(bubbleList):
    listLength = len(bubbleList)
    while listLength > 0:
        for in range(listLength - 1):
            if bubbleList[i] > bubbleList[i+1]:
                bubbleList[i] = bubbleList[i] + bubbleList[i+1]
                bubbleList[i+1= bubbleList[i] - bubbleList[i+1]
                bubbleList[i] = bubbleList[i] - bubbleList[i+1]
        listLength -= 1
    print bubbleList
 
if __name__ == '__main__':
    bubbleList = [3412580]
    bubble(bubbleList)

百度上直接搜索到的那個是錯的。

這個又簡單又好懂。

listLength = len(bubbleList) 這行命令能提高運行效率。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章