-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainn.py
More file actions
250 lines (193 loc) · 7.57 KB
/
mainn.py
File metadata and controls
250 lines (193 loc) · 7.57 KB
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
import pygame
import random
import math
from time import sleep
pygame.init()
class Drawinfo:
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
GREY = (128, 128, 128)
DARK_PINK = (255, 255, 255)
BACKGROUND_COLOR = WHITE
clock = pygame.time.Clock()
GREDIENT = [
(128, 128, 128),
(160, 160, 160),
(192, 192, 192),
]
FONT = pygame.font.SysFont('comicsans', 30)
LARGE_FONT = pygame.font.SysFont('comicsans', 30)
side_pad = 100
top_pad = 100
def __init__(self, width, height, lst):
self.width = width
self.height = height
self.window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Sorting Algorithm & Visualization")
self.set_list(lst)
def set_list(self, lst):
self.lst = lst
self.max_val = max(lst)
self.min_val = min(lst)
self.block_width = round((self.width - self.side_pad) / len(lst))
self.block_height = math.floor(
(self.height - self.top_pad) / (self.max_val - self.min_val)
)
self.start_x = self.side_pad // 2
# -----------------------------------------------------------------------------------------------------------
#generate randrom list of elements.
def generate_starting_list(n, min_val, max_val):
return [random.randint(min_val, max_val) for _ in range(n)]
# -----------------------------------------------------------------------------------------------------------
# Draws each bar (value rectangles) | Colors the bars (normal or highlighted colors during sorting)
# Displays numbers on each bar | Optionally clears only the bar-area (for smooth animation)
# Draw_list() draws ONLY the bars section — used during sorting.
def draw_list(draw_info, color_position={}, clear_bg=False):
if clear_bg:
clear_rect = (
draw_info.side_pad // 2,
draw_info.top_pad,
draw_info.width - draw_info.side_pad,
draw_info.height - draw_info.top_pad
)
pygame.draw.rect(draw_info.window, draw_info.BACKGROUND_COLOR, clear_rect)
lst = draw_info.lst
for i, val in enumerate(lst):
x = draw_info.start_x + i * draw_info.block_width
height = (val - draw_info.min_val) * draw_info.block_height
y = draw_info.height - height
color = draw_info.GREDIENT[i % 3]
if i in color_position:
color = color_position[i]
pygame.draw.rect(
draw_info.window,
color,
(x, y, draw_info.block_width, height)
)
if val > 15:
small_font = pygame.font.SysFont('comicsans', 14)
text = small_font.render(str(val), True, (255, 255, 255))
text_x = x + draw_info.block_width // 2 - text.get_width() // 2 + 3
text_y = y + 2
draw_info.window.blit(text, (text_x, text_y))
if clear_bg:
pygame.display.update()
# -----------------------------------------------------------------------------------------------------------
# Clears the window | Draws the top control text | Draws all bars (by calling draw_list()) | Updates the display
def draw(draw_info, fps, paused):
draw_info.window.fill(draw_info.BACKGROUND_COLOR)
controls = draw_info.FONT.render(
"R-Reset | SPACE-Start | A-Asc | D-Desc | TAB-Pause | B-Bubble | I-Insertion",
1,
draw_info.BLACK
)
draw_info.window.blit(
controls,
(draw_info.width/2 - controls.get_width()/2, 5)
)
draw_list(draw_info)
pygame.display.update()
# ---------------------- BUBBLE SORT ------------------------
def bubble_sort(draw_info, ascending=True, paused=None):
lst = draw_info.lst
for i in range(len(lst) - 1):
for j in range(len(lst) - i - 1):
while paused and paused[0]:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_TAB:
paused[0] = False
if event.type == pygame.QUIT:
pygame.quit()
exit()
sleep(0.05)
if (lst[j] > lst[j + 1] and ascending) or \
(lst[j] < lst[j + 1] and not ascending):
lst[j], lst[j + 1] = lst[j + 1], lst[j]
draw_list(draw_info, {j: draw_info.RED, j+1: draw_info.GREEN}, True)
yield i
# ---------------------- INSERTION SORT ------------------------
def insertion_sort(draw_info, ascending=True, paused=None):
lst = draw_info.lst
for i in range(1, len(lst)):
key = lst[i]
j = i - 1
while j >= 0 and ((lst[j] > key and ascending) or (lst[j] < key and not ascending)):
while paused and paused[0]:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_TAB:
paused[0] = False
if event.type == pygame.QUIT:
pygame.quit()
exit()
sleep(0.05)
lst[j + 1] = lst[j]
j -= 1
draw_list(draw_info, {j+1: draw_info.RED, i: draw_info.GREEN}, True)
yield True
lst[j + 1] = key
draw_list(draw_info, {j+1: draw_info.GREEN, i: draw_info.RED}, True)
yield True
# ---------------------- MAIN LOOP ------------------------
def main():
run = True
clock = pygame.time.Clock()
paused = [False]
fps = 30
n = 30
min_val = 15
max_val = 100
lst = generate_starting_list(n, min_val, max_val)
draw_info = Drawinfo(800, 700, lst)
sorting = False
ascending = True
sorting_algorithm = bubble_sort
sorting_algo_generator = None
while run:
clock.tick(fps)
if sorting:
try:
next(sorting_algo_generator)
except StopIteration:
sorting = False
else:
draw(draw_info, fps, paused)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type != pygame.KEYDOWN:
continue
# will stop and Reset Values in List for Sorting
if event.key == pygame.K_r:
lst = generate_starting_list(n, min_val, max_val)
draw_info.set_list(lst)
sorting = False
#Start the sorting
elif event.key == pygame.K_SPACE and not sorting:
sorting = True
sorting_algo_generator = sorting_algorithm(draw_info, ascending, paused)
#Start sorting Ascending
elif event.key == pygame.K_a and not sorting:
ascending = True
#Start sorting descending
elif event.key == pygame.K_d and not sorting:
ascending = False
# Pause toggle (Sorting on UI)
elif event.key == pygame.K_TAB:
paused[0] = not paused[0]
# Increase speed To visualize sort
elif event.key == pygame.K_RIGHT:
fps = min(60, fps + 5)
# Decrease speed To visualize sort
elif event.key == pygame.K_LEFT:
fps = max(3, fps - 5)
# sort selection Bubble sort
elif event.key == pygame.K_b:
sorting_algorithm = bubble_sort
# sort selection Insertion sort
elif event.key == pygame.K_i:
sorting_algorithm = insertion_sort
pygame.quit()
if __name__ == "__main__":
main()