Stable sort using python bubble sort

So, I got a list of tuples for sorting in order of ints. What I am missing makes stable sorting.

How can I create bubble stabilization? (keep order for 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, which is expected, and y resault: Display of the result from element 0 expected: [('h6', 7), ('h5', 7), ('h2', 8), ('h3', 8), ( 'h7', 15), ('h9', 24), ('h4', 30), ('h8', 54), ('h1', 72)] In fact: [('h6', 7), ('h5', 7), ('h3', 8), ('h2', 8), ('h7', 15), ('h9', 24), ('h4', 30), (' h8 ', 54), (' h1 ', 72)] result_code bubble_14 wrong 1

h2/3... ..

+4
1

, , , . , . .

, , , . , , , .

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)

, , 100% - . , , , .

+1

All Articles