Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import random
- a = None
- def insertion_sort():
- global a
- a = sort()
- show()
- def show():
- global a
- if a is not None:
- try:
- next(a)
- window.after(10, show)
- except:
- a = None
- finally:
- window.after_cancel(show)
- def swap(pos_0, pos_1):
- Bar1x1, _, Bar1x2, _ = canvas.coords(pos_0)
- Bar2x1, _, Bar2x2, _ = canvas.coords(pos_1)
- canvas.move(pos_0, Bar2x1-Bar1x1, 0)
- canvas.move(pos_1, Bar1x2-Bar2x2, 0)
- def sort():
- global listt
- global lengthList
- for i in range(len(lengthList)):
- cursor = lengthList[i]
- cursorBar = listt[i]
- pos = i
- while pos > 0 and lengthList[pos - 1] > cursor:
- lengthList[pos] = lengthList[pos - 1]
- listt[pos], listt[pos - 1] =listt[pos - 1], listt[pos]
- swap(listt[pos],listt[pos-1])
- yield
- pos -= 1
- lengthList[pos] = cursor
- listt[pos] = cursorBar
- swap(listt[pos],cursorBar)
- def mix():
- global listt
- global lengthList
- canvas.delete('all')
- xstart = 5
- xend = 15
- listt = []
- lengthList = []
- for x in range(1, 60):
- randomY = random.randint(1, 390)
- x = canvas.create_rectangle(xstart, randomY, xend, 395, fill='powderblue')
- listt.append(x)
- xstart += 10
- xend += 10
- for bar in listt:
- x = canvas.coords(bar)
- length = x[3] - x[1]
- lengthList.append(length)
- window = tk.Tk()
- window.title('Insertion Sort Sorting ALGORITHM')
- window.geometry('600x450')
- canvas = tk.Canvas(window, width='600', height='400')
- canvas.grid(column=0,row=0, columnspan = 50)
- insert = tk.Button(window, text='SORT', command=insertion_sort)
- shuf = tk.Button(window, text='MIX', command=mix)
- insert.grid(column=49,row=1)
- shuf.grid(column=0, row=1)
- mix()
- window.mainloop()
- #https://www.youtube.com/channel/UCQor7IURWM-lGT-tmFbFSCw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement