所以我对python很陌生,我有一个项目需要我们经历一个很长的元组列表,我们必须按降序和升序排列列表。但是,对于我的两个功能,我总是按升序排列,什么是错误的?有人请帮助我真的压力太大了在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](http://docs.python.org/2/library/functions.html#sorted)? – thefourtheye
@thefourtheye我猜这是一个学习练习。 –
它正在为我排序正确,降序排列。你确定你发布了两个函数吗? – arocks