【发布时间】:2014-01-16 03:23:28
【问题描述】:
所以我是 python 新手,我有一个项目需要我们通过一个非常长的元组列表,我们必须按降序和升序对列表进行排序。但是,对于我的两个函数,我总是得到升序,有什么问题?有人请帮助我真的压力很大
def bubblesort_descending(tuple_list):
j = len(tuple_list)
made_swap = True
swaps = 0
while made_swap:
made_swap = False
for cnt in range (j-1):
if tuple_list[cnt] < tuple_list[cnt+1]:
tuple_list[cnt], tuple_list[cnt+1] = tuple_list[cnt+1], tuple_list[cnt]
made_swap = True
swaps = swaps + 1
return swaps
主程序:
elif choice == 'd':
unsorted = range(len(numbers))
shuffle(unsorted)
print ("Randomised tuple list generated:")
print
print (unsorted)
swaps = bubblesort_descending (unsorted)
print
print ("heres the sorted list")
print
print (unsorted)
print
print (swaps, "swap(s) made")
print
【问题讨论】:
-
你为什么不用sorted?
-
@thefourtheye 我猜这是一个学习练习。
-
它对我来说是正确的,按降序排列。你确定你发布了这两个函数吗?
-
是的,它的工作人员我忘了“返回元组列表”哈哈谢谢你的帮助!!
标签: python algorithm bubble-sort