所以,我得到了我的元组列表,按整数的顺序排序。我缺少的是使排序稳定..
如何使冒泡排序稳定?(保持类似物品的顺序)
def bubble_sort_2nd_value(tuples_list):
NEWLIST = []
ITEM_MOVE = 0
for i in tuples_list:
NEWLIST.append(i)
for i in range(len(NEWLIST)):
for j in range(i+1, len(NEWLIST)):
if(NEWLIST[j][1] < NEWLIST[i][1]):
ITEM_MOVE = 1
NEWLIST[j],NEWLIST[i] = NEWLIST[i],NEWLIST[j]
if (ITEM_MOVE == 0):
print(tuples_list)
else:
print(NEWLIST)
tuples_list = [('h2', 8), ('h4', 30), ('h6', 7), ('h8', 54), ('h1', 72), ('h3', 8), ('h5', 7), ('h7', 15), ('h7', 24)]
bubble_sort_2nd_value(tuples_list)
预期的测试器结果和y结果比较:显示预期元素0的输出:[('h6',7),('h5',7),('h2',8),('h3',8), ('h7', 15), ('h9', 24), ('h4', 30), ('h8', 54), ('h1', 72)] 实际:[('h6', 7) , ('h5', 7), ('h3', 8), ('h2', 8), ('h7', 15), ('h9', 24), ('h4', 30), ( 'h8', 54), ('h1', 72)] result_code bubble_14 错误 1
注意 h2/3 混合...需要修复它..我的意思是不稳定