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 bubbleListif __name__ == '__main__': bubbleList = [3, 4, 1, 2, 5, 8, 0] bubble(bubbleList) |
百度上直接搜索到的那个是错的。
这个又简单又好懂。
listLength = len(bubbleList)
这行命令能提高运行效率。
本文提供了一个简单易懂的冒泡排序算法Python实现,并通过一个具体示例展示了如何使用该算法进行排序。文章中还强调了一种能提高运行效率的方法。
937

被折叠的 条评论
为什么被折叠?



