I have been trying to make a bubble sort visualization in python using tkinter, but it does not work. When I put in only a few bars, nothing happens, but if try it with lots of bars, they are moving, seemingly as they should be (at first, at least). My question is, is there a mistake in my logic that I do not see, or is it just tkinter? I am starting to think the latter. Thanks in advance. Here's my code:
import tkinter
import time
import random
width = 800
height = 800
canvas = tkinter.Canvas(height=height, width=width, background='black')
canvas.pack()
thickness = 20
values = [random.randrange(height) for i in range(width//thickness)]
cc = {}
for value in values:
cc[value] = 0
print(values)
x1 = 0
for value in values:
canvas.create_rectangle(x1,800, x1+thickness,value, fill='white', tag=value)
cc[value] = x1
x1 += thickness
for i in range(len(values)):
for j in range(len(values)-i-1):
if values[j] > values[j+1]:
values[j], values[j+1] = values[j+1], values[j]
canvas.move(values[j], cc[values[j+1]] - cc[values[j]], 0)
canvas.move(values[j+1], cc[values[j]]- cc[values[j+1]], 0)
cc[values[j]] += (cc[values[j+1]]- cc[values[j]])
cc[values[j+1]] += (cc[values[j]]- cc[values[j+1]])
canvas.update()
canvas.after(50)
canvas.update()
else:
canvas.after(5)
canvas.mainloop()
print(values)