-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.py
358 lines (290 loc) · 13.1 KB
/
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
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
import pygame
import os
from Algorithms.nodes.lines import Line
from Algorithms.nodes.button import button
from Sorts.bead import bead
from Sorts.bitonic import bitonic
from Sorts.bogo import bogo
from Sorts.bubble import bubble
from Sorts.bucket import bucket
from Sorts.cocktail import cocktail
from Sorts.comb import comb
from Sorts.counting import counting
from Sorts.cycle import cycle
from Sorts.double import double
from Sorts.gnome import gnome
from Sorts.heap import heap
from Sorts.insertion import insertion
from Sorts.merge import iterativemerge
from Sorts.oddeven import oddeven
from Sorts.pancake import pancake
from Sorts.pigeon import pigeon
from Sorts.quick import quick
from Sorts.radix import radix
from Sorts.shell import shell
from Sorts.selection import selection
from Sorts.sleep import sleepsort
from Sorts.stooge import stooge
from Sorts.wiggle import wiggle
from random import shuffle, randint, choice
pygame.init()
FILEPATH = os.getcwd()
# infoObject = pygame.display.Info()
WIDTH = 1584
COLUMNS = 256
HEIGHT = 784
WHITE = (255,255,255)
BLACK = (0,0,0)
WIN = pygame.display.set_mode((1584, 784))
pygame.display.set_caption("Visualiser: Edit")
def make_lines():
grid = []
for i in range(1, COLUMNS+1):
node = Line(i, i-1)
grid.append(node)
return grid
def draw_grid(win):
gap = WIDTH / COLUMNS
for i in range(COLUMNS+1):
pygame.draw.line(win, WHITE, (i*gap, 0), (i*gap, HEIGHT))
def draw(win, grid, t):
win.fill(WHITE)
for row in grid:
row.draw(WIN)
if t:
draw_grid(win)
pygame.display.update()
def sortloop():
# print((infoObject.current_w, infoObject.current_h))
grid = make_lines()
pygame.init()
run = True
started = False
already = False
togglegrid = True
l = [i for i in range(1,COLUMNS+1)]
shuffle(l)
for node in grid:
g = choice(l)
node.evalu(g)
l.remove(g)
while run:
if not started:
draw(WIN, grid, togglegrid)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1 and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Bead Sort")
run, already = bead(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_2 and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Bitonic Sort")
run, already = bitonic(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_3 and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Bogo Sort")
run, already = bogo(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_4 and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Bubble Sort")
run, already = bubble(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_5 and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Bucket Sort")
run, already = bucket(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_6 and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Cocktail Shaker Sort")
run, already = cocktail(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_7 and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Comb Sort")
run, already = comb(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_8 and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Counting Sort")
run, already = counting(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_9 and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Cycle Sort")
run, already = cycle(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_0 and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Double Sort")
run, already = double(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_q and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Gnome Sort")
run, already = gnome(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_w and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Heap Sort")
run, already = heap(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_e and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Insertion Sort")
run, already = insertion(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_r and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Merge Sort")
run, already = iterativemerge(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_u and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Odd Even Sort")
run, already = oddeven(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_y and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Pancake Sort")
run, already = pancake(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_u and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Pigeonhole Sort")
run, already = pigeon(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_i and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Quick Sort")
run, already = quick(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_o and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Radix Sort")
run, already = radix(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_a and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Shell Sort")
run, already = shell(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_p and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Selection Sort")
run, already = selection(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_s and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Sleep Sort")
run, already = sleepsort(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_d and not started and not already:
started = True
pygame.display.set_caption("Visualiser: Stooge Sort")
run, already = stooge(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_f and not started:
started = True
pygame.display.set_caption("Visualiser: Wiggle Sort")
run = wiggle(lambda : draw(WIN, grid, togglegrid), grid)
started = False
if event.key == pygame.K_SPACE:
l = [i for i in range(1,COLUMNS+1)]
shuffle(l)
for node in grid:
g = choice(l)
node.evalu(g)
l.remove(g)
pygame.display.set_caption("Visualiser: Shuffled")
already = False
if event.key == pygame.K_RSHIFT:
l = [i for i in range(1,COLUMNS+1)]
l = l[::-1]
for node in grid:
g = l.pop(0)
node.evalu(g + 1)
pygame.display.set_caption("Visualiser: Inverted")
already = False
if event.key == pygame.K_ESCAPE:
pygame.display.set_caption("Visualiser: Main Page")
return False
if event.key == pygame.K_TAB:
togglegrid = not togglegrid
if event.key == pygame.K_h:
helppage()
def invert(color):
a, b, c = color
return (abs(255 - a), abs(255 - b), abs(255 - c))
def helppage(k=False):
pygame.init()
Quit = False
inv = False
background1 = pygame.image.load(os.path.join(FILEPATH, 'Fonts', 'Help1.png'))
nextpage = button((0, 0, 0), 1440, 350, 75, 100, FILEPATH, (0, 0, 0), text='>')
while not Quit:
if k:
return
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if nextpage.isOver(pos):
k = helppage2()
continue
if event.type == pygame.MOUSEMOTION:
if nextpage.isOver(pos):
inv = True
else:
inv = False
WIN.blit(background1, (0, 0))
fillhelp(inv, nextpage)
def helppage2():
pygame.init()
Quit = False
inv = False
inv1 = False
background2 = pygame.image.load(os.path.join(FILEPATH, 'Fonts', 'Help2.png'))
prevpage = button((0, 0, 0), 1440, 350, 75, 100, FILEPATH, (0, 0, 0), text='<')
while not Quit:
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if prevpage.isOver(pos):
return True
if event.type == pygame.MOUSEMOTION:
if prevpage.isOver(pos):
inv1 = True
else:
inv1 = False
WIN.blit(background2, (0, 0))
fillhelp(inv1, prevpage)
def fillhelp(inv2, btn2):
color = WHITE
if inv2:
btn2.textcolor = invert(color)
btn2.color = color
else:
btn2.textcolor = color
btn2.color = invert(color)
btn2.draw(WIN, btn2.textcolor)
pygame.display.update()
if __name__ == "__main__":
sortloop()