[SOLVED][not swapping]Need help with Comb Sort visualzation

from SkyScrapperClasSS import Sky

width = 1000
height = width/2
num = 100
heights = []
counter = 0
gap = num*10/13
swapped = False
caounter = 0
getg = False
r = color(255,0,0)

def setup():
    size(width,height)
    for i in range(num):
        heights.append(Sky(i*10,random (height)))



def draw():
    global gap,getg,counter,swapped
    print(gap)
    background(0)
    swapped = False
    
    if getg:
        gap = gapper(gap)

    for i in range(num):
        if getg:
            getg = False
        if counter >= num - gap and not getg:
            getg = True
        if counter == i or counter == i + gap:
            text(heights[i].x/10,heights[i].x,10)
            fill(255,0,0)
        else:
            fill(255)
        heights[i].show()
        if not(counter >= num - gap):
            if heights[counter].y > heights[counter + gap].y:
                swapped = True
                print("swappin")
                heights[counter].x, heights[counter + gap].x =  heights[counter + gap].x, heights[counter].x

    counter += 1
    if not swapped and gap == 1:
        print("Printin :")
        for i in range(num):
            print(heights[i].x,heights[i].y)
        noLoop()


        
        
def gapper(gap):
    gap = int((gap*10) / 13)
    if gap < 1:
        return 1
    return gap
class Sky:
    def __init__(self,x,y):
        self.x = x
        self.y = y

    def show(self):
        rect(self.x,height-self.y,10,self.y)
        

(I know that this code is a mess, a class is too much i could’ve used an array of pvectors and other stuff)The point is, i am swapping the X coordinate of a rect if his height is higer,and when i print theyr X, i can see the X actually swapped, but on the screen nothing changes

:frowning_face:

1 Like

Remove your call to noLoop() at the end of draw(). Do you still have the same problem?

Yeap, I still have this problem

Here is a guess. Set your initial gap to 1. That should make your comb sort perform its final (bubble sort) pass, correct?

What happens instead? Why?

I’ve read again my code now, and I’ve just seen that the problem was that I wasnt converting to int() everytime I was recalculating the gap :crazy_face: