-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizer.py
316 lines (285 loc) · 10.4 KB
/
visualizer.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import pygame
import random
from math import ceil
class Visualizer:
pygame.init()
def __init__(self, size=720):
self.sorting = False
self.ascending = True
self.bars = 64
self.tick = 64
self.width = size // 2 * 3
self.height = size
self.top_pad = self.height / 4
self.font_size = self.height // 24
self.window = pygame.display.set_mode((self.width, self.height))
pygame.display.set_caption("Sorting Algorithm Visualizer")
self.setAlgo()
self.genList()
self.update()
self.gen = self.algo()
def genList(self):
self.list = [100 / self.bars * (i + 1) for i in range(self.bars)]
random.shuffle(self.list)
self.bar_spacing = self.width / self.bars
self.bar_width = ceil(self.bar_spacing)
self.bar_height = (self.height - self.top_pad) // 100
def changeTick(self, up):
if up and self.tick < 256:
self.tick *= 2
elif not up and self.tick > 4:
self.tick //= 2
def changeBars(self, up):
if up and self.bars < 256:
self.bars *= 2
elif not up and self.bars > 4:
self.bars //= 2
self.genList()
def setAlgo(self, key=False):
self.algorithms = {
pygame.K_b: ("Bubble Sort", self.bubbleSort),
pygame.K_i: ("Insertion Sort", self.insertionSort),
pygame.K_m: ("Merge Sort", self.mergeSort),
pygame.K_q: ("Quick Sort", self.quickSort),
pygame.K_c: ("Crazy Sort", self.crazySort),
pygame.K_s: ("Selection Sort", self.selectionSort),
}
if not key:
key = list(self.algorithms.keys())[0]
self.algo_name = self.algorithms[key][0]
self.algo = self.algorithms[key][1]
# Visualization
def update(self):
self.window.fill((0, 0, 0))
play_pause = "Pause" if self.sorting else "Play"
titles1 = [
f"ARROWS - {self.bars} Bars {self.tick} Tick",
f"SPACE - {play_pause}",
"R - Reset",
]
for i, title in enumerate(titles1):
self.window.blit(
pygame.font.Font(None, self.font_size).render(
title,
1,
(255, 255, 255),
),
(self.width // 3, 10 + self.font_size * i),
)
titles2 = [
"A - Ascending",
"D - Descending",
]
for i, title in enumerate(titles2):
color = 255, 255, 255
if title[0] == "A" and self.ascending:
color = 0, 255, 255
if title[0] == "D" and not self.ascending:
color = 0, 255, 255
self.window.blit(
pygame.font.Font(None, self.font_size).render(
title,
1,
color,
),
(self.width // 3 * 2, 10 + self.font_size * i),
)
algorithms = [
f"{name[0]} - {name}" for name in (x for x, _ in self.algorithms.values())
]
for i, algo in enumerate(algorithms):
color = 255, 255, 255
if algo[0] == self.algo_name[0]:
color = 255, 0, 255
self.window.blit(
pygame.font.Font(None, self.font_size).render(
algo,
1,
color,
),
(10, 10 + self.font_size * i),
)
self.drawList()
def drawList(self, clear_bg=False):
if clear_bg:
pygame.draw.rect(
self.window,
(0, 0, 0),
(
0,
self.top_pad,
self.width,
self.height,
),
)
for i, val in enumerate(self.list):
x = i * self.bar_spacing
y = self.height - val * self.bar_height
color = (
val * 2.55,
(100 - val) * 2.55,
255,
)
pygame.draw.rect(
self.window,
color,
(x, y, self.bar_width, self.height),
)
pygame.display.update()
# Algorithms
def bubbleSort(self):
for i in range(self.bars - 1):
for j in range(self.bars - 1 - i):
num1 = self.list[j]
num2 = self.list[j + 1]
if (num1 > num2 and self.ascending) or (
num1 < num2 and not self.ascending
):
self.list[j], self.list[j + 1] = self.list[j + 1], self.list[j]
yield True
def insertionSort(self):
for i in range(1, self.bars):
current = self.list[i]
while True:
ascending_sort = i > 0 and self.list[i - 1] > current and self.ascending
descending_sort = (
i > 0 and self.list[i - 1] < current and not self.ascending
)
if not (ascending_sort or descending_sort):
break
self.list[i], self.list[i - 1] = self.list[i - 1], current
i -= 1
yield True
def selectionSort(self):
for i in range(self.bars - 1):
k = i
for j in range(i + 1, self.bars):
if (
self.list[j] < self.list[k]
and self.ascending
or self.list[j] > self.list[k]
and not self.ascending
):
k = j
self.list[i], self.list[k] = self.list[k], self.list[i]
yield True
def mergeSort(self, start=0, end=False):
if not end:
end = self.bars
if end - start > 1:
mid = (start + end) // 2
yield from self.mergeSort(start, mid)
yield from self.mergeSort(mid, end)
left = self.list[start:mid]
right = self.list[mid:end]
a = 0
b = 0
c = start
while a < len(left) and b < len(right):
if (left[a] < right[b] and self.ascending) or (
left[a] > right[b] and not self.ascending
):
self.list[c] = left[a]
a += 1
else:
self.list[c] = right[b]
b += 1
c += 1
yield True
while a < len(left):
self.list[c] = left[a]
a += 1
c += 1
yield True
while b < len(right):
self.list[c] = right[b]
b += 1
c += 1
yield True
def quickSort(self, low=0, high=None):
if high is None:
high = self.bars - 1
partition = self.quickSortPartition(low, high)
while low < high:
while True:
try:
pi = next(partition)
break
except StopIteration:
partition = self.quickSortPartition(low, high)
break
if pi:
if pi - low < high - pi:
yield from self.quickSort(low, pi - 1)
low = pi + 1
else:
yield from self.quickSort(pi + 1, high)
high = pi - 1
else:
yield True
def quickSortPartition(self, l, h):
pivot = self.list[h]
i = l - 1
for j in range(l, h):
if (self.list[j] <= pivot and self.ascending) or (
self.list[j] > pivot and not self.ascending
):
i = i + 1
(self.list[i], self.list[j]) = (self.list[j], self.list[i])
yield False
(self.list[i + 1], self.list[h]) = (self.list[h], self.list[i + 1])
yield i + 1
def crazySort(self):
while not (
self.list == sorted(self.list)
if self.ascending
else self.list == sorted(self.list, reverse=True)
):
random.shuffle(self.list)
yield True
# Main function
def run(self):
clock = pygame.time.Clock()
while True:
clock.tick(self.tick)
if self.sorting:
try:
next(self.gen)
self.drawList(clear_bg=True)
except StopIteration:
self.sorting = False
self.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == pygame.KEYDOWN:
key = event.key
if key == pygame.K_SPACE:
self.sorting = False if self.sorting else True
elif key == pygame.K_DOWN:
self.changeTick(False)
elif key == pygame.K_UP:
self.changeTick(True)
else:
if key == pygame.K_r:
self.genList()
self.sorting = False
elif key == pygame.K_a:
self.ascending = True
elif key == pygame.K_d:
self.ascending = False
elif key == pygame.K_LEFT:
self.changeBars(False)
self.sorting = False
elif key == pygame.K_RIGHT:
self.changeBars(True)
self.sorting = False
elif key in self.algorithms.keys():
self.setAlgo(key)
else:
continue
self.gen = self.algo()
self.update()
if __name__ == "__main__":
Visualizer().run()