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 i 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 = [ 3 , 4 , 1 , 2 , 5 , 8 , 0 ] bubble(bubbleList) |
百度上直接搜索到的那個是錯的。
這個又簡單又好懂。
listLength
=
len
(bubbleList)
這行命令能提高運行效率。