-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sotrs Visualistaion.py
183 lines (154 loc) · 6.04 KB
/
Sotrs Visualistaion.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import tkinter as tk
from random import randint
class SortsVisualisation(tk.Frame):
sorters = ["bubble_sort", "insertion_sort", "sel_sort", "shell_sort", "quick_sort", "heapsort"]
def __init__(self, root, width, height, bg="white"):
super().__init__(root, width=width, height=height, bg=bg)
self.root = root
self.root.title("Sorts visualization")
self.cnv = tk.Canvas(root, width=width, height=height, bg=bg)
self.width = width
self.height = height
self.array = []
self.graphic_array = []
self.steps = False
self.latency = 0
self.changer = tk.Button(root, text="by steps on", command=self.steps_change, fg="#0fab60", bg="#3c3f41")
self.changer.place(x=width - 90, y=height - 100)
x = 10
y = height - 45
for sort in SortsVisualisation.sorters:
tk.Button(self.cnv, text=sort, width=10, fg="white", bg="#3c3f41",
command=lambda s=sort: self.start_sort(eval(f"self.{s}"))).place(x=x, y=y)
x += 100
def generate(self):
self.array.clear()
self.graphic_array.clear()
set_array = [i for i in range(self.width)]
while set_array:
self.array.append(set_array.pop(randint(0, len(set_array) - 1)))
self.graphic_array = [self.cnv.create_line(i, 0, i, self.array[i], fill="#91313d") for i in
range(len(self.array))]
def _mark(self, obj1, obj2, color):
self.cnv.itemconfig(obj1, fill=color)
self.cnv.itemconfig(obj2, fill=color)
def motion(func):
def wrapper(self, *args):
for l1, l2 in func(self, *args):
self.cnv.move(self.graphic_array[l1], l2 - l1, 0)
self.cnv.move(self.graphic_array[l2], l1 - l2, 0)
if self.steps:
self._mark(self.graphic_array[l1], self.graphic_array[l2], "yellow")
self.cnv.update()
self._mark(self.graphic_array[l1], self.graphic_array[l2], "#91313d")
self.cnv.after(self.latency)
self.graphic_array[l1], self.graphic_array[l2] = self.graphic_array[l2], self.graphic_array[l1]
return wrapper
@motion
def bubble_sort(self):
for i in range(len(self.array)):
for j in range(len(self.array) - i - 1):
if self.array[j] < self.array[j + 1]:
self.array[j], self.array[j + 1] = self.array[j + 1], self.array[j]
yield j, j + 1
self.cnv.update()
@motion
def sel_sort(self):
for i in range(len(self.array) - 1):
mi = i
for j in range(i + 1, len(self.array)):
if self.array[mi] < self.array[j]:
mi = j
self.array[i], self.array[mi] = self.array[mi], self.array[i]
yield i, mi
self.cnv.update()
@motion
def insertion_sort(self, start=0, gap=1):
for i in range(start + gap, len(self.array), gap):
j = i
while j >= gap and self.array[j] > self.array[j - gap]:
self.array[j - gap], self.array[j] = self.array[j], self.array[j - gap]
j -= gap
yield j, j + gap
self.cnv.update()
def shell_sort(self):
subl_count = (len(self.array) - 1) // 3
while subl_count > 0:
for start_position in range(subl_count):
self.insertion_sort(start_position, subl_count)
subl_count = (subl_count - 1) // 3
def partition(self, low, high):
pivot = self.array[(low + high) // 2]
if self.steps:
self.cnv.itemconfig(self.graphic_array[-pivot], fill="green")
self.cnv.update()
i = low - 1
j = high + 1
while True:
i += 1
while self.array[i] > pivot:
i += 1
j -= 1
while self.array[j] < pivot:
j -= 1
if i >= j:
self.cnv.itemconfig(self.graphic_array[-pivot], fill="#91313d")
return j
self.array[i], self.array[j] = self.array[j], self.array[i]
yield i, j
self.cnv.update()
def quick_sort(self):
@SortsVisualisation.motion
def _quick_sort(self, low, high):
if low < high:
it = iter(self.partition(low, high))
while True:
try:
yield next(it)
except StopIteration as e:
split_index = e.value
break
_quick_sort(self, low, split_index)
_quick_sort(self, split_index + 1, high)
_quick_sort(self, 0, len(self.array) - 1)
@motion
def heapify(self, arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[i] > arr[l]:
largest = l
if r < n and arr[largest] > arr[r]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
yield i, largest
self.heapify(arr, n, largest)
self.cnv.update()
@motion
def heapsort(self):
n = len(self.array)
for i in range(n, -1, -1):
self.heapify(self.array, n, i)
for i in range(n - 1, 0, -1):
self.array[i], self.array[0] = self.array[0], self.array[i]
yield i, 0
self.heapify(self.array, i, 0)
def start_sort(self, func):
self.cnv.delete("all")
self.generate()
self.cnv.update()
func()
def steps_change(self):
if self.steps:
self.steps = False
self.changer.config(fg="#0fab60", text="by steps on")
else:
self.steps = True
self.changer.config(fg="#91313d", text="by steps off")
self.changer.update()
def run(self):
self.cnv.pack()
self.root.mainloop()
if __name__ == "__main__":
SortsVisualisation(tk.Tk(), 600, 600, "#2b2b2b").run()