简体   繁体   English

使用python冒泡排序稳定排序

[英]stable sorting using python bubble sort

So, I got my list of tuples to sort out by order of ints. 所以,我得到了我的元组列表,按顺序排序。 What I'm missing is making the sort stable.. 我缺少的是让排序稳定..

How can i make bubble sort stable? 我怎样才能使泡泡排序稳定? (keep order on similar items) (保持对类似物品的订单)

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)

tester resault which is expected and y resault comparison : Showing output from element 0 expected: [('h6', 7), ('h5', 7), ('h2', 8), ('h3', 8), ('h7', 15), ('h9', 24), ('h4', 30), ('h8', 54), ('h1', 72)] actual: [('h6', 7), ('h5', 7), ('h3', 8), ('h2', 8), ('h7', 15), ('h9', 24), ('h4', 30), ('h8', 54), ('h1', 72)] result_code bubble_14 wrong 1 预期的测试人员冲突和y灾难比较:显示预期的元素0的输出:[('h6',7),('h5',7),('h2',8),('h3',8), ('h7',15),('h9',24),('h4',30),('h8',54),('h1',72)] actual:[('h6',7) ,('h5',7),('h3',8),('h2',8),('h7',15),('h9',24),('h4',30),( 'h8',54),('h1',72)] result_code bubble_14错误1

notice the h2/3 mix... need to fix it.. that what i mean by not stable 注意h2 / 3混合...需要修复它...我的意思是不稳定

The best thing you could do right now is to understand why it is switching those items. 你现在能做的最好的事情就是明白为什么要切换这些物品。 Print at every step what items are being changed. 每一步都打印哪些项目正在更改。 Then you might understand the behaviour. 然后你可能会理解这种行为。

So I had a go at it, you don't compare the current item with the next item in the list, you compare the current item with all the following items in the list. 所以我去了,你没有比较当前项目与列表中的下一项,你将当前项目与列表中的所有以下项目进行比较。 The changes I made bellow might give you the result you want. 我所做出的改变可能会给你你想要的结果。

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(len(NEWLIST)-1):
             k=j+1
             if(NEWLIST[j][1] > NEWLIST[k][1]):
                 ITEM_MOVE = 1
                 NEWLIST[j],NEWLIST[k] = NEWLIST[k],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)

So what you are doing is not 100% bubble sort. 所以你所做的不是100%冒泡排序。 Try this and tell me if you need me to explain why yours is not working. 试试这个并告诉我你是否需要我解释你为什么不工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM