- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.py
More file actions
Latest commit
330 lines (258 loc) · 10.1 KB
/
Copy pathsorting.py
File metadata and controls
330 lines (258 loc) · 10.1 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
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
# sorting.py
# imports
importos
importmath
importtime
importjson
importpickle
importrandom
importpygame
fromcollectionsimportdeque
# globals
fromuiimportUI
fromglobyimport*
# pygame gui
importpygame_gui
frompygame_gui.elementsimportUILabel
frompygame_gui.elementsimportUIImage
frompygame_gui.elementsimportUIPanel
frompygame_gui.elementsimportUIWindow
frompygame_gui.elementsimportUIButton
frompygame_gui.elementsimportUITextBox
frompygame_gui.elementsimportUIDropDownMenu
frompygame_gui.windowsimportUIMessageWindow
frompygame_gui.elementsimportUISelectionList
frompygame_gui.elementsimportUITextEntryLine
frompygame_guiimportUIManager, PackageResource
frompygame_gui.elementsimportUIHorizontalSlider
frompygame_gui.elementsimportUIScreenSpaceHealthBar
classSorting:
def__init__(self, ui, s=Options.default_sorting_size):
self.ui=ui
self.size=s
self.set_size(s)
self.delay=0
self.running=False
# merge sort
self.merge_array= []
# heap sort
self.heap= []
self.heap_last=-1
defset_size(self, s):
self.size=s
self.reset()
defreset(self):
self.fill_colours= [Options.sorting_c_map[0] for_inrange(self.size)]
self.temp_colours= [Nonefor_inrange(self.size)]
self.bar_width=Options.main_width/self.size
self.heights= [(i+1) * (Options.main_height/self.size) foriinrange(self.size)]
ifOptions.sorting_shuffle:
random.shuffle(self.heights)
defprocess_slider(self, x):
new_size= (x*10) -30
# condition
ifnew_size>256:
new_size=256
ifself.size!=new_size:
self.set_size(new_size)
defprocess_button(self, state):
ifstate==True:
self.ui.disable()
self.start=True
defrender(self):
# self.ui.draw_background()
fori, colour, heightinzip(range(self.size), self.fill_colours, self.heights):
bar=pygame.Rect(Options.main_x+ (i*self.bar_width), Options.main_height-height,
self.bar_width, height)
# adjust bar in x
next_left=Options.main_x+ (i+1) *self.bar_widthifi!=self.size-1elseOptions.resolution[0]
diff=next_left- (bar.left+bar.width)
bar.width+=diff
bar.width-=Options.bar_gap
# adjust bar in y
diff=Options.main_height- (bar.top+bar.height)
bar.height+=diff
pygame.draw.rect(self.ui.get_window(), colour, bar)
pygame.display.update()
ifself.delay!=0: pygame.time.delay(self.delay)
defheap_sort(self):
self.heap= [-1] *self.size
self.heap_last=-1
# initial heapify
self.heapify()
# show actual colour order
foriinrange(self.size-1, -1, -1):
self.fill_colours[i] =Options.sorting_c_map[1]
self.update_render()
self.fill_colours[i] =self.temp_colours[i]
self.update_render()
# remove n times
removed= []
foriinrange(self.size):
removed.append(self.heap_remove(v=True)) # remove with visualization
self.fill_colours[i] =Options.sorting_c_map[0]
self.update_render()
# show fully sorted
foriinrange(self.size-1, -1, -1):
self.fill_colours[i] =Options.sorting_c_map[3]
self.update_render()
defheapify(self):
foriinrange(self.size-1, -1, -1):
self.heap_add(self.heights[i], i)
# assign actual colours
forhinrange(self.size):
current_height=int(math.ceil(math.log(h+2, 2.0)))
colour=Options.heap_sort_colours[current_height-1]
self.temp_colours[self.heap[h][1]] =colour
defheap_add(self, x, index):
self.heap_last+=1
self.heap[self.heap_last] = [x, index]
self.heap_trickle_up(self.heap_last, True)
defheap_remove(self, v=False):
ifself.heap_last==-1:
return
return_value=self.heap[0]
self.heap_swap(0, self.heap_last, v, r=True)
self.heap_last-=1
self.heap_trickle_down(0, v)
returnreturn_value
defheap_trickle_up(self, pos, v=False):
ifpos==0:
return
parent= (pos-1) //2
ifself.heap[pos][0] >self.heap[parent][0]:
self.heap_swap(pos, parent, v)
returnself.heap_trickle_up(parent, v)
defheap_trickle_down(self, parent, v=False):
left= (2*parent) +1
right= (2*parent) +2
# check leaf (no children)
ifleft>self.heap_lastandright>self.heap_last:
return
# no right child
ifright>self.heap_last:
ifself.heap[parent][0] <self.heap[left][0]:
self.heap_swap(parent, left, v)
returnself.heap_trickle_down(left, v)
else:
return
# has both children
ifself.heap[parent][0] <self.heap[left][0] orself.heap[parent][0] <self.heap[right][0]:
ifself.heap[left][0] >self.heap[right][0]:
self.heap_swap(parent, left, v)
self.heap_trickle_down(left, v)
else:
self.heap_swap(parent, right, v)
self.heap_trickle_down(right, v)
defheap_swap(self, p1, p2, v=False, r=False):
self.heap[p1], self.heap[p2] =self.heap[p2], self.heap[p1]
self.heap[p1][1], self.heap[p2][1] =self.heap[p2][1], self.heap[p1][1] # switch indices
# swap in bars
temp_1=self.fill_colours[self.heap[p1][1]]
temp_2=self.fill_colours[self.heap[p2][1]] ifnotrelseOptions.sorting_c_map[2]
self.fill_colours[self.heap[p1][1]] =Options.sorting_c_map[1]
self.fill_colours[self.heap[p2][1]] =Options.sorting_c_map[1]
self.update_render()
# switch heights
self.heights[self.heap[p1][1]], self.heights[self.heap[p2][1]], =self.heights[self.heap[p2][1]], self.heights[self.heap[p1][1]]
self.update_render()
self.fill_colours[self.heap[p1][1]] =temp_1
self.fill_colours[self.heap[p2][1]] =temp_2
self.update_render()
defmerge(self, a, b, c, d):
i=a
j=c
merge_array= []
whilei<=bandj<=d:
self.fill_colours[i] =Options.sorting_c_map[1]
self.fill_colours[j] =Options.sorting_c_map[1]
self.update_render()
self.fill_colours[i] =Options.sorting_c_map[0]
self.fill_colours[j] =Options.sorting_c_map[0]
# merge in ascending
ifself.heights[i] <self.heights[j]:
merge_array.append(self.heights[i])
i+=1
else:
merge_array.append(self.heights[j])
j+=1
# append remaining from left
whilei<=b:
self.fill_colours[i] =Options.sorting_c_map[1]
self.update_render()
self.fill_colours[i] =Options.sorting_c_map[0]
merge_array.append(self.heights[i])
i+=1
# append remaining from right
whilej<=d:
self.fill_colours[j] =Options.sorting_c_map[1]
self.update_render()
self.fill_colours[j] =Options.sorting_c_map[0]
merge_array.append(self.heights[j])
j+=1
j=0
foriinrange(a, d+1):
self.heights[i] =merge_array[j]
j+=1
self.fill_colours[i] =Options.sorting_c_map[2]
self.update_render()
# final show
ifd-a==self.size-1:
self.fill_colours[i] =Options.sorting_c_map[3]
else:
self.fill_colours[i] =Options.sorting_c_map[0]
# recursive merge sort
defmerge_sort(self, l, r):
mid= (l+r) //2
ifl<r:
self.merge_sort(l, mid)
self.merge_sort(mid+1, r)
self.merge(l, mid, mid+1, r)
defupdate(self):
# process events
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
self.running=False
pygame.quit()
exit()
self.ui.manager.process_events(event)
# process ui events
self.process_slider(self.ui.get_slider_value())
self.process_button(self.ui.button_pressed())
# update-render
self.ui.update()
self.ui.render()
defupdate_render(self):
self.update()
self.render()
defreset_colours(self):
self.fill_colours= [Options.sorting_c_map[0]] *self.size
defrun(self) ->bool:
self.running=True
self.start=False
self.ui.disable_extras()
whileself.runningandself.ui.get_dropdown_topic() ==Options.topics[0]:
self.update()
# visualize
ifnotself.start:
self.render()
else:
ifself.ui.get_dropdown_algo() ==Options.sorting_options[0]:
self.reset_colours()
self.delay=int((Options.frame_rate//Options.sorting_fps_delay_ratio) * (1))
# merge sort
self.merge_sort(0, self.size-1)
# # do a final show (as sanjay said)
# for i in range(self.size):
# self.fill_colours[i] = Options.heap_sort_colours[-1]
elifself.ui.get_dropdown_algo() ==Options.sorting_options[1]:
self.reset_colours()
self.delay=int((Options.frame_rate//Options.sorting_fps_delay_ratio) * (0.5))
# heap sort
self.heap_sort()
self.delay=0
# restart ui
self.ui.enable()
self.start=False
self.running=True# cannot close the program
returnself.running