forked from Azanul/Sorting-Visualized
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_sort.py
68 lines (30 loc) · 788 Bytes
/
heap_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from OFA import *
def heapify(list, n, i):
largest = i
l = 2*i+1
r = 2*i+2
if l < n and list[i].h < list[l].h:
largest = l
if r < n and list[largest].h < list[r].h:
largest = r
if largest != i:
swap(i, largest, list)
time.sleep(0.03)
heapify(list, n, largest)
def sort(list=blocks):
n = len(list)
for i in range(n, -1, -1):
heapify(list, n, i)
for i in range(n-1, 0, -1):
swap(i, 0, list)
time.sleep(0.03)
heapify(list, i, 0)
if __name__ == "__main__":
tk.title("Heap Sort")
blocks=[]
generate()
btn1 = Button(tk, text = 'Shuffle', bd = '5', command = lambda: shuffle())
btn2 = Button(tk, text = 'Sort', bd = '5', command = lambda: sort())
btn1.pack(side='left')
btn2.pack(side='left')
tk.mainloop()