【发布时间】:2017-09-02 19:19:18
【问题描述】:
我开始研究数据结构和算法,并尝试实现冒泡排序:
def BubbleSort(list):
for a in range(len(list)):
for b in range(len(list)):# I could start this loop from 1
if list[a]< list[b]: # to avoid comparing the first element twice
temp=list[a]
list[a]=list[b]
list[b]=temp
return list
我浏览了网络和书籍 - 但没有找到冒泡排序的 Python 实现。 以上有什么问题?
【问题讨论】:
-
“没有这样的实现”是什么意思?从某种意义上说,它会以相反的方式对列表进行排序,并且它需要大约 2 倍的必要时间,而且它不是冒泡排序。
-
我的意思是冒泡排序没有这样的实现,但正如你所说的,它不是冒泡排序。
-
I browsed the net速读?看看Q&A@CR。
标签: python algorithm python-3.x sorting