1

My code almost works, but I suspect something is being overwritten in my enumerations. I noticed when debugging that I'm comparing the same indexes for ex i=0 and j=0 or i = 1 and j=1. Could this be the issue to my problem? If so how can I adjust my enumerations? enter image description here

full code: https://pastebin.com/UaEssaSP


def start_algo(bars, tick_time):

    #first bar in list
    #canvas.itemconfig(bars[0], fill='red')
    #canvas.itemconfig(bars[49], fill='blue')

   
    #Bubble sort
    for i, _ in enumerate(bars):
        _, y1_low ,_ ,_ = canvas.coords(bars[i])  #unpack
        for j, _ in enumerate(bars): 
                _, y1_curr, _ , _ = canvas.coords(bars[j])  #unpack
                if y1_curr < y1_low:
                    swap_bars(bars[i],bars[j])
                    bars[i], bars[j] = bars[j], bars[i]
                    
    


Jamesyt60
  • 57
  • 6

1 Answers1

2

Your algorithm doesn't look like bubble sort. This way it seems to work fine:

for i in range(len(bars)-1, -1, -1):
    for j in range(0, i):
        _, y1_low ,_ ,_ = canvas.coords(bars[j+1])
        _, y1_curr, _ , _ = canvas.coords(bars[j])
        if y1_curr < y1_low:
            swap_bars(bars[j],bars[j+1])
            bars[j+1], bars[j] = bars[j], bars[j+1]

result:

enter image description here

  • 1
    Thank you. What kind of sort is this if not a bubble? – Jamesyt60 Aug 02 '23 at 23:18
  • 1
    You are comparing every pair possible and swapping if the current fixed one is larger than the sweeping one. It will not sort the bars. You can make your algorithm sort the bars if you change the condition for swapping to "y1_curr < y1_low and i > j". I don't know any algorithm by name that works like this. – Delta de Dirac Aug 02 '23 at 23:29