-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
430 lines (342 loc) · 13.1 KB
/
main.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import numpy
import pygame
import math
import random
import os
import sys
import time
import colorsys
import concurrent.futures
class Tile:
def __init__(self, coord1, coord2=0, color=[0, 0, 0]):
if isinstance(coord1, int):
self.x = coord1
self.y = coord2
else:
self.x, self.y = coord1
self.coord = [self.x, self.y]
self.color = self.update_color(color)
r, g, b = self.color
self.hue = int(colorsys.rgb_to_hsv(r, g, b)[0] * 360)
def __repr__(self):
return f"Tile at {self.x}, {self.y} with {self.hue} degree"
def update_color(self, color):
try:
if self.color != color:
time.sleep(step_time)
except AttributeError:
pass
self.color = color
pygame.draw.rect(screen, color, (self.x * tile_width, self.y * tile_height, tile_width, tile_height))
pygame.display.update((self.x * tile_width, self.y * tile_height, tile_width, tile_height))
r, g, b = self.color
self.hue = int(colorsys.rgb_to_hsv(r, g, b)[0] * 360)
return self.color
def selection_sort():
with concurrent.futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
for y in range(grid_height):
executor.submit(selection_sort_helper, y)
def selection_sort_helper(y):
for x in range(grid_width):
min_x = x
for next_x in range(x + 1, grid_width):
if grid[min_x, y].hue > grid[next_x, y].hue:
min_x = next_x
swap(grid[x, y], grid[min_x, y])
def insertion_sort():
with concurrent.futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
for y in range(grid_height):
executor.submit(insertion_sort_helper, y)
def insertion_sort_helper(y):
for x in range(1, grid_width):
current_hue = grid[x, y].hue
current_color = grid[x, y].color
x_before = x - 1
while x_before >= 0 and current_hue < grid[x_before, y].hue:
grid[x_before + 1, y].update_color(grid[x_before, y].color)
x_before -= 1
grid[x_before + 1, y].update_color(current_color)
def bubble_sort():
with concurrent.futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
for y in range(grid_height):
executor.submit(bubble_sort_helper, y)
def bubble_sort_helper(y):
for x in range(grid_width):
swapped = False
for current_x in range(0, grid_width - x - 1):
if grid[current_x, y].hue > grid[current_x + 1, y].hue:
swap(grid[current_x, y], grid[current_x + 1, y])
swapped = True
if not swapped:
break
def merge_sort():
with concurrent.futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
for y in range(grid_height):
executor.submit(merge_sort_helper, range(0, grid_width), y)
def merge_sort_helper(indices, row):
if len(indices) > 1:
mid = len(indices) // 2
left = indices[:mid]
right = indices[mid:]
merge_sort_helper(left, row)
merge_sort_helper(right, row)
left = [[grid[index, row].color, grid[index, row].hue] for index in left]
right = [[grid[index, row].color, grid[index, row].hue] for index in right]
cur_l = cur_r = cur_overall = 0
while cur_l < len(left) and cur_r < len(right):
if left[cur_l][1] < right[cur_r][1]:
grid[indices[cur_overall], row].update_color(left[cur_l][0])
cur_l += 1
else:
grid[indices[cur_overall], row].update_color(right[cur_r][0])
cur_r += 1
cur_overall += 1
while cur_l < len(left):
grid[indices[cur_overall], row].update_color(left[cur_l][0])
cur_l += 1
cur_overall += 1
while cur_r < len(right):
grid[indices[cur_overall], row].update_color(right[cur_r][0])
cur_r += 1
cur_overall += 1
def quick_sort_last():
with concurrent.futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
for y in range(grid_height):
executor.submit(quick_sort_last_helper, 0, grid_width - 1, y)
def quick_sort_last_helper(first_index, last_index, row):
if first_index < last_index:
partition_index = partition_last(first_index, last_index, row)
quick_sort_last_helper(first_index, partition_index - 1, row)
quick_sort_last_helper(partition_index + 1, last_index, row)
def partition_last(first_index, last_index, row):
pivot = grid[last_index, row]
smaller_index = first_index - 1
for current_index in range(first_index, last_index):
if grid[current_index, row].hue < pivot.hue:
smaller_index += 1
swap(grid[smaller_index, row], grid[current_index, row])
swap(grid[smaller_index + 1, row], grid[last_index, row])
return smaller_index + 1
def quick_sort_random():
with concurrent.futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
for y in range(grid_height):
executor.submit(quick_sort_random_helper, 0, grid_width - 1, y)
def quick_sort_random_helper(first_index, last_index, row):
if first_index < last_index:
partition_index = partition_random_r(first_index, last_index, row)
quick_sort_random_helper(first_index, partition_index - 1, row)
quick_sort_random_helper(partition_index + 1, last_index, row)
def partition_random_r(first_index, last_index, row):
random_pivot = random.randrange(first_index, last_index)
swap(grid[first_index, row], grid[random_pivot, row])
return partition_random(first_index, last_index, row)
def partition_random(first_index, last_index, row):
pivot = grid[first_index, row]
smaller_index = first_index + 1
for current_index in range(first_index + 1, last_index + 1):
if grid[current_index, row].hue < pivot.hue:
swap(grid[smaller_index, row], grid[current_index, row])
smaller_index += 1
swap(pivot, grid[smaller_index - 1, row])
return smaller_index - 1
def multi_algorithm():
with concurrent.futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
for y in range(0, grid_height, 4):
executor.submit(bubble_sort_helper, y)
executor.submit(insertion_sort_helper, y + 1)
executor.submit(merge_sort_helper, list(range(0, grid_width)), y + 2)
executor.submit(selection_sort_helper, y + 3)
def bucket_sort():
with concurrent.futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
for y in range(grid_height):
executor.submit(bucket_sort_helper, y)
def bucket_sort_helper(row):
sorted_references = []
buckets = int(grid_width ** .5)
for i in range(buckets):
sorted_references.append([])
for x in range(grid_width):
sorted_references[int(buckets * (grid[x, row].hue / 360))].append(grid[x, row])
for j in range(buckets):
insertion_sort_bucket(sorted_references[j], row)
for j in range(buckets):
sorted_references[j] = [tile.color for tile in sorted_references[j]]
x = 0
for i in range(buckets):
for j in range(len(sorted_references[i])):
grid[x, row].update_color(sorted_references[i][j])
x += 1
def radix_sort():
with concurrent.futures.ThreadPoolExecutor(max_workers=max_threads) as executor:
for y in range(grid_height):
executor.submit(radix_sort_helper, y)
def radix_sort_helper(row):
references = []
for x in range(grid_width):
references.append(grid[x, row])
maximum = max([tile.hue for tile in references])
exp = 1
while maximum // exp > 0:
counting_sort(exp, references)
exp *= 10
def counting_sort(place, references):
output = [0] * len(references)
count = [0] * 10
for i in range(len(references)):
index = references[i].hue // place
count[index % 10] += 1
for i in range(1, 10):
count[i] += count[i - 1]
for i in range(len(references) - 1, -1, -1):
index = references[i].hue // place
output[count[index % 10] - 1] = references[i]
count[index % 10] -= 1
output = [tile.color for tile in output]
for i in range(0, len(references)):
references[i].update_color(output[i])
def insertion_sort_bucket(arr, row):
for index in range(1, len(arr)):
current_hue = arr[index].hue
current_color = arr[index].color
last_index = index - 1
while last_index >= 0 and current_hue < arr[last_index].hue:
grid[arr[last_index + 1].x, row].update_color(arr[last_index].color)
last_index -= 1
grid[arr[last_index + 1].x, row].update_color(current_color)
def swap(tile1, tile2):
temp = tile2.color[:]
tile2.update_color(tile1.color)
tile1.update_color(temp)
def change_sort(index):
global current_sort_index
if index == "next":
if current_sort_index >= len(sorts) - 1:
current_sort_index = 0
else:
current_sort_index += 1
elif index == "back":
if current_sort_index <= 0:
current_sort_index = len(sorts) - 1
else:
current_sort_index += -1
else:
current_sort_index = index
pygame.display.set_caption(f"{sort_names[current_sort_index]} Sort")
def change_increment(index):
global current_increment_index, grid_width, display_width, tile_width, tile_height, screen, colors, grid
if index == "next":
if current_increment_index >= len(increments) - 1:
current_increment_index = 0
else:
current_increment_index += 1
elif index == "back":
if current_increment_index <= 0:
current_increment_index = len(increments) - 1
else:
current_increment_index += -1
else:
current_increment_index = index
grid_width = 255 // increments[current_increment_index][0] * 6
display_width = increments[current_increment_index][1]
tile_width = display_width // grid_width
tile_height = display_height // grid_height
screen = pygame.display.set_mode((display_width, display_height))
colors = make_colors()
grid = make_board()
def make_colors():
result = []
for i in range(3):
for j in range(3):
temp_color = [0, 0, 0]
temp_color[i] = 255
if j != i:
for k in range(0, 256, increments[current_increment_index][0]):
temp_color[j] = k
if temp_color not in result:
result.append(temp_color[:])
print(len(result))
return result
def make_board():
result_grid = numpy.empty((grid_width, grid_height), object)
for y in range(grid_height):
temp = colors[:]
for x in range(grid_width):
result_grid[x, y] = Tile(x, y, temp.pop(random.randint(0, len(temp) - 1)))
return result_grid
sort_names = [
"Selection",
"Insertion",
"Bubble",
"Merge",
"Quick (Last Item Pivot)",
"Quick (Random Item Pivot)",
"Bucket",
"Radix",
"Multi-Algorithm"
]
sorts = [
selection_sort,
insertion_sort,
bubble_sort,
merge_sort,
quick_sort_last,
quick_sort_random,
bucket_sort,
radix_sort,
multi_algorithm
]
increments = [
[85, 360],
[51, 600],
[17, 900],
[15, 918],
[5, 918],
[3, 1020]
]
current_increment_index = 0
grid_width = 255 // increments[current_increment_index][0] * 6
grid_height = 8
display_width = increments[current_increment_index][1]
display_height = 360
tile_width = display_width // grid_width
tile_height = display_height // grid_height
default_step_time = 1 / 500
step_show = True
step_time = default_step_time
max_threads = 4
os.environ['SDL_VIDEO_CENTERED'] = "0"
pygame.init()
screen = pygame.display.set_mode((display_width, display_height))
current_sort_index = 0
pygame.display.set_caption(f"{sort_names[current_sort_index]} Sort")
colors = make_colors()
grid = make_board()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_F5:
print("Resetting Board")
grid = make_board()
print("Board Reset")
if event.key == pygame.K_RETURN:
print("Sorting")
start = time.time()
sorts[current_sort_index]()
elapsed = time.time() - start
print(f"Sorted in {elapsed} second(s)")
if event.key == pygame.K_RIGHT:
change_sort("next")
if event.key == pygame.K_LEFT:
change_sort("back")
if event.key == pygame.K_UP:
change_increment("next")
if event.key == pygame.K_DOWN:
change_increment("back")
if event.key == pygame.K_s:
step_show = not step_show
step_time = default_step_time if step_show else 0
print(f"Showing steps is {step_show}")
pygame.display.flip()