1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """ |
---|
4 | n/a | Sorting algorithms visualizer using Tkinter. |
---|
5 | n/a | |
---|
6 | n/a | This module is comprised of three ``components'': |
---|
7 | n/a | |
---|
8 | n/a | - an array visualizer with methods that implement basic sorting |
---|
9 | n/a | operations (compare, swap) as well as methods for ``annotating'' the |
---|
10 | n/a | sorting algorithm (e.g. to show the pivot element); |
---|
11 | n/a | |
---|
12 | n/a | - a number of sorting algorithms (currently quicksort, insertion sort, |
---|
13 | n/a | selection sort and bubble sort, as well as a randomization function), |
---|
14 | n/a | all using the array visualizer for its basic operations and with calls |
---|
15 | n/a | to its annotation methods; |
---|
16 | n/a | |
---|
17 | n/a | - and a ``driver'' class which can be used as a Grail applet or as a |
---|
18 | n/a | stand-alone application. |
---|
19 | n/a | """ |
---|
20 | n/a | |
---|
21 | n/a | from tkinter import * |
---|
22 | n/a | import random |
---|
23 | n/a | |
---|
24 | n/a | XGRID = 10 |
---|
25 | n/a | YGRID = 10 |
---|
26 | n/a | WIDTH = 6 |
---|
27 | n/a | |
---|
28 | n/a | |
---|
29 | n/a | class Array: |
---|
30 | n/a | |
---|
31 | n/a | class Cancelled(BaseException): |
---|
32 | n/a | pass |
---|
33 | n/a | |
---|
34 | n/a | def __init__(self, master, data=None): |
---|
35 | n/a | self.master = master |
---|
36 | n/a | self.frame = Frame(self.master) |
---|
37 | n/a | self.frame.pack(fill=X) |
---|
38 | n/a | self.label = Label(self.frame) |
---|
39 | n/a | self.label.pack() |
---|
40 | n/a | self.canvas = Canvas(self.frame) |
---|
41 | n/a | self.canvas.pack() |
---|
42 | n/a | self.report = Label(self.frame) |
---|
43 | n/a | self.report.pack() |
---|
44 | n/a | self.left = self.canvas.create_line(0, 0, 0, 0) |
---|
45 | n/a | self.right = self.canvas.create_line(0, 0, 0, 0) |
---|
46 | n/a | self.pivot = self.canvas.create_line(0, 0, 0, 0) |
---|
47 | n/a | self.items = [] |
---|
48 | n/a | self.size = self.maxvalue = 0 |
---|
49 | n/a | if data: |
---|
50 | n/a | self.setdata(data) |
---|
51 | n/a | |
---|
52 | n/a | def setdata(self, data): |
---|
53 | n/a | olditems = self.items |
---|
54 | n/a | self.items = [] |
---|
55 | n/a | for item in olditems: |
---|
56 | n/a | item.delete() |
---|
57 | n/a | self.size = len(data) |
---|
58 | n/a | self.maxvalue = max(data) |
---|
59 | n/a | self.canvas.config(width=(self.size+1)*XGRID, |
---|
60 | n/a | height=(self.maxvalue+1)*YGRID) |
---|
61 | n/a | for i in range(self.size): |
---|
62 | n/a | self.items.append(ArrayItem(self, i, data[i])) |
---|
63 | n/a | self.reset("Sort demo, size %d" % self.size) |
---|
64 | n/a | |
---|
65 | n/a | speed = "normal" |
---|
66 | n/a | |
---|
67 | n/a | def setspeed(self, speed): |
---|
68 | n/a | self.speed = speed |
---|
69 | n/a | |
---|
70 | n/a | def destroy(self): |
---|
71 | n/a | self.frame.destroy() |
---|
72 | n/a | |
---|
73 | n/a | in_mainloop = 0 |
---|
74 | n/a | stop_mainloop = 0 |
---|
75 | n/a | |
---|
76 | n/a | def cancel(self): |
---|
77 | n/a | self.stop_mainloop = 1 |
---|
78 | n/a | if self.in_mainloop: |
---|
79 | n/a | self.master.quit() |
---|
80 | n/a | |
---|
81 | n/a | def step(self): |
---|
82 | n/a | if self.in_mainloop: |
---|
83 | n/a | self.master.quit() |
---|
84 | n/a | |
---|
85 | n/a | def wait(self, msecs): |
---|
86 | n/a | if self.speed == "fastest": |
---|
87 | n/a | msecs = 0 |
---|
88 | n/a | elif self.speed == "fast": |
---|
89 | n/a | msecs = msecs//10 |
---|
90 | n/a | elif self.speed == "single-step": |
---|
91 | n/a | msecs = 1000000000 |
---|
92 | n/a | if not self.stop_mainloop: |
---|
93 | n/a | self.master.update() |
---|
94 | n/a | id = self.master.after(msecs, self.master.quit) |
---|
95 | n/a | self.in_mainloop = 1 |
---|
96 | n/a | self.master.mainloop() |
---|
97 | n/a | self.master.after_cancel(id) |
---|
98 | n/a | self.in_mainloop = 0 |
---|
99 | n/a | if self.stop_mainloop: |
---|
100 | n/a | self.stop_mainloop = 0 |
---|
101 | n/a | self.message("Cancelled") |
---|
102 | n/a | raise Array.Cancelled |
---|
103 | n/a | |
---|
104 | n/a | def getsize(self): |
---|
105 | n/a | return self.size |
---|
106 | n/a | |
---|
107 | n/a | def show_partition(self, first, last): |
---|
108 | n/a | for i in range(self.size): |
---|
109 | n/a | item = self.items[i] |
---|
110 | n/a | if first <= i < last: |
---|
111 | n/a | self.canvas.itemconfig(item, fill='red') |
---|
112 | n/a | else: |
---|
113 | n/a | self.canvas.itemconfig(item, fill='orange') |
---|
114 | n/a | self.hide_left_right_pivot() |
---|
115 | n/a | |
---|
116 | n/a | def hide_partition(self): |
---|
117 | n/a | for i in range(self.size): |
---|
118 | n/a | item = self.items[i] |
---|
119 | n/a | self.canvas.itemconfig(item, fill='red') |
---|
120 | n/a | self.hide_left_right_pivot() |
---|
121 | n/a | |
---|
122 | n/a | def show_left(self, left): |
---|
123 | n/a | if not 0 <= left < self.size: |
---|
124 | n/a | self.hide_left() |
---|
125 | n/a | return |
---|
126 | n/a | x1, y1, x2, y2 = self.items[left].position() |
---|
127 | n/a | ## top, bot = HIRO |
---|
128 | n/a | self.canvas.coords(self.left, (x1 - 2, 0, x1 - 2, 9999)) |
---|
129 | n/a | self.master.update() |
---|
130 | n/a | |
---|
131 | n/a | def show_right(self, right): |
---|
132 | n/a | if not 0 <= right < self.size: |
---|
133 | n/a | self.hide_right() |
---|
134 | n/a | return |
---|
135 | n/a | x1, y1, x2, y2 = self.items[right].position() |
---|
136 | n/a | self.canvas.coords(self.right, (x2 + 2, 0, x2 + 2, 9999)) |
---|
137 | n/a | self.master.update() |
---|
138 | n/a | |
---|
139 | n/a | def hide_left_right_pivot(self): |
---|
140 | n/a | self.hide_left() |
---|
141 | n/a | self.hide_right() |
---|
142 | n/a | self.hide_pivot() |
---|
143 | n/a | |
---|
144 | n/a | def hide_left(self): |
---|
145 | n/a | self.canvas.coords(self.left, (0, 0, 0, 0)) |
---|
146 | n/a | |
---|
147 | n/a | def hide_right(self): |
---|
148 | n/a | self.canvas.coords(self.right, (0, 0, 0, 0)) |
---|
149 | n/a | |
---|
150 | n/a | def show_pivot(self, pivot): |
---|
151 | n/a | x1, y1, x2, y2 = self.items[pivot].position() |
---|
152 | n/a | self.canvas.coords(self.pivot, (0, y1 - 2, 9999, y1 - 2)) |
---|
153 | n/a | |
---|
154 | n/a | def hide_pivot(self): |
---|
155 | n/a | self.canvas.coords(self.pivot, (0, 0, 0, 0)) |
---|
156 | n/a | |
---|
157 | n/a | def swap(self, i, j): |
---|
158 | n/a | if i == j: return |
---|
159 | n/a | self.countswap() |
---|
160 | n/a | item = self.items[i] |
---|
161 | n/a | other = self.items[j] |
---|
162 | n/a | self.items[i], self.items[j] = other, item |
---|
163 | n/a | item.swapwith(other) |
---|
164 | n/a | |
---|
165 | n/a | def compare(self, i, j): |
---|
166 | n/a | self.countcompare() |
---|
167 | n/a | item = self.items[i] |
---|
168 | n/a | other = self.items[j] |
---|
169 | n/a | return item.compareto(other) |
---|
170 | n/a | |
---|
171 | n/a | def reset(self, msg): |
---|
172 | n/a | self.ncompares = 0 |
---|
173 | n/a | self.nswaps = 0 |
---|
174 | n/a | self.message(msg) |
---|
175 | n/a | self.updatereport() |
---|
176 | n/a | self.hide_partition() |
---|
177 | n/a | |
---|
178 | n/a | def message(self, msg): |
---|
179 | n/a | self.label.config(text=msg) |
---|
180 | n/a | |
---|
181 | n/a | def countswap(self): |
---|
182 | n/a | self.nswaps = self.nswaps + 1 |
---|
183 | n/a | self.updatereport() |
---|
184 | n/a | |
---|
185 | n/a | def countcompare(self): |
---|
186 | n/a | self.ncompares = self.ncompares + 1 |
---|
187 | n/a | self.updatereport() |
---|
188 | n/a | |
---|
189 | n/a | def updatereport(self): |
---|
190 | n/a | text = "%d cmps, %d swaps" % (self.ncompares, self.nswaps) |
---|
191 | n/a | self.report.config(text=text) |
---|
192 | n/a | |
---|
193 | n/a | |
---|
194 | n/a | class ArrayItem: |
---|
195 | n/a | |
---|
196 | n/a | def __init__(self, array, index, value): |
---|
197 | n/a | self.array = array |
---|
198 | n/a | self.index = index |
---|
199 | n/a | self.value = value |
---|
200 | n/a | self.canvas = array.canvas |
---|
201 | n/a | x1, y1, x2, y2 = self.position() |
---|
202 | n/a | self.item_id = array.canvas.create_rectangle(x1, y1, x2, y2, |
---|
203 | n/a | fill='red', outline='black', width=1) |
---|
204 | n/a | self.canvas.tag_bind(self.item_id, '<Button-1>', self.mouse_down) |
---|
205 | n/a | self.canvas.tag_bind(self.item_id, '<Button1-Motion>', self.mouse_move) |
---|
206 | n/a | self.canvas.tag_bind(self.item_id, '<ButtonRelease-1>', self.mouse_up) |
---|
207 | n/a | |
---|
208 | n/a | def delete(self): |
---|
209 | n/a | item_id = self.item_id |
---|
210 | n/a | self.array = None |
---|
211 | n/a | self.item_id = None |
---|
212 | n/a | self.canvas.delete(item_id) |
---|
213 | n/a | |
---|
214 | n/a | def mouse_down(self, event): |
---|
215 | n/a | self.lastx = event.x |
---|
216 | n/a | self.lasty = event.y |
---|
217 | n/a | self.origx = event.x |
---|
218 | n/a | self.origy = event.y |
---|
219 | n/a | self.canvas.tag_raise(self.item_id) |
---|
220 | n/a | |
---|
221 | n/a | def mouse_move(self, event): |
---|
222 | n/a | self.canvas.move(self.item_id, |
---|
223 | n/a | event.x - self.lastx, event.y - self.lasty) |
---|
224 | n/a | self.lastx = event.x |
---|
225 | n/a | self.lasty = event.y |
---|
226 | n/a | |
---|
227 | n/a | def mouse_up(self, event): |
---|
228 | n/a | i = self.nearestindex(event.x) |
---|
229 | n/a | if i >= self.array.getsize(): |
---|
230 | n/a | i = self.array.getsize() - 1 |
---|
231 | n/a | if i < 0: |
---|
232 | n/a | i = 0 |
---|
233 | n/a | other = self.array.items[i] |
---|
234 | n/a | here = self.index |
---|
235 | n/a | self.array.items[here], self.array.items[i] = other, self |
---|
236 | n/a | self.index = i |
---|
237 | n/a | x1, y1, x2, y2 = self.position() |
---|
238 | n/a | self.canvas.coords(self.item_id, (x1, y1, x2, y2)) |
---|
239 | n/a | other.setindex(here) |
---|
240 | n/a | |
---|
241 | n/a | def setindex(self, index): |
---|
242 | n/a | nsteps = steps(self.index, index) |
---|
243 | n/a | if not nsteps: return |
---|
244 | n/a | if self.array.speed == "fastest": |
---|
245 | n/a | nsteps = 0 |
---|
246 | n/a | oldpts = self.position() |
---|
247 | n/a | self.index = index |
---|
248 | n/a | newpts = self.position() |
---|
249 | n/a | trajectory = interpolate(oldpts, newpts, nsteps) |
---|
250 | n/a | self.canvas.tag_raise(self.item_id) |
---|
251 | n/a | for pts in trajectory: |
---|
252 | n/a | self.canvas.coords(self.item_id, pts) |
---|
253 | n/a | self.array.wait(50) |
---|
254 | n/a | |
---|
255 | n/a | def swapwith(self, other): |
---|
256 | n/a | nsteps = steps(self.index, other.index) |
---|
257 | n/a | if not nsteps: return |
---|
258 | n/a | if self.array.speed == "fastest": |
---|
259 | n/a | nsteps = 0 |
---|
260 | n/a | myoldpts = self.position() |
---|
261 | n/a | otheroldpts = other.position() |
---|
262 | n/a | self.index, other.index = other.index, self.index |
---|
263 | n/a | mynewpts = self.position() |
---|
264 | n/a | othernewpts = other.position() |
---|
265 | n/a | myfill = self.canvas.itemcget(self.item_id, 'fill') |
---|
266 | n/a | otherfill = self.canvas.itemcget(other.item_id, 'fill') |
---|
267 | n/a | self.canvas.itemconfig(self.item_id, fill='green') |
---|
268 | n/a | self.canvas.itemconfig(other.item_id, fill='yellow') |
---|
269 | n/a | self.array.master.update() |
---|
270 | n/a | if self.array.speed == "single-step": |
---|
271 | n/a | self.canvas.coords(self.item_id, mynewpts) |
---|
272 | n/a | self.canvas.coords(other.item_id, othernewpts) |
---|
273 | n/a | self.array.master.update() |
---|
274 | n/a | self.canvas.itemconfig(self.item_id, fill=myfill) |
---|
275 | n/a | self.canvas.itemconfig(other.item_id, fill=otherfill) |
---|
276 | n/a | self.array.wait(0) |
---|
277 | n/a | return |
---|
278 | n/a | mytrajectory = interpolate(myoldpts, mynewpts, nsteps) |
---|
279 | n/a | othertrajectory = interpolate(otheroldpts, othernewpts, nsteps) |
---|
280 | n/a | if self.value > other.value: |
---|
281 | n/a | self.canvas.tag_raise(self.item_id) |
---|
282 | n/a | self.canvas.tag_raise(other.item_id) |
---|
283 | n/a | else: |
---|
284 | n/a | self.canvas.tag_raise(other.item_id) |
---|
285 | n/a | self.canvas.tag_raise(self.item_id) |
---|
286 | n/a | try: |
---|
287 | n/a | for i in range(len(mytrajectory)): |
---|
288 | n/a | mypts = mytrajectory[i] |
---|
289 | n/a | otherpts = othertrajectory[i] |
---|
290 | n/a | self.canvas.coords(self.item_id, mypts) |
---|
291 | n/a | self.canvas.coords(other.item_id, otherpts) |
---|
292 | n/a | self.array.wait(50) |
---|
293 | n/a | finally: |
---|
294 | n/a | mypts = mytrajectory[-1] |
---|
295 | n/a | otherpts = othertrajectory[-1] |
---|
296 | n/a | self.canvas.coords(self.item_id, mypts) |
---|
297 | n/a | self.canvas.coords(other.item_id, otherpts) |
---|
298 | n/a | self.canvas.itemconfig(self.item_id, fill=myfill) |
---|
299 | n/a | self.canvas.itemconfig(other.item_id, fill=otherfill) |
---|
300 | n/a | |
---|
301 | n/a | def compareto(self, other): |
---|
302 | n/a | myfill = self.canvas.itemcget(self.item_id, 'fill') |
---|
303 | n/a | otherfill = self.canvas.itemcget(other.item_id, 'fill') |
---|
304 | n/a | if self.value < other.value: |
---|
305 | n/a | myflash = 'white' |
---|
306 | n/a | otherflash = 'black' |
---|
307 | n/a | outcome = -1 |
---|
308 | n/a | elif self.value > other.value: |
---|
309 | n/a | myflash = 'black' |
---|
310 | n/a | otherflash = 'white' |
---|
311 | n/a | outcome = 1 |
---|
312 | n/a | else: |
---|
313 | n/a | myflash = otherflash = 'grey' |
---|
314 | n/a | outcome = 0 |
---|
315 | n/a | try: |
---|
316 | n/a | self.canvas.itemconfig(self.item_id, fill=myflash) |
---|
317 | n/a | self.canvas.itemconfig(other.item_id, fill=otherflash) |
---|
318 | n/a | self.array.wait(500) |
---|
319 | n/a | finally: |
---|
320 | n/a | self.canvas.itemconfig(self.item_id, fill=myfill) |
---|
321 | n/a | self.canvas.itemconfig(other.item_id, fill=otherfill) |
---|
322 | n/a | return outcome |
---|
323 | n/a | |
---|
324 | n/a | def position(self): |
---|
325 | n/a | x1 = (self.index+1)*XGRID - WIDTH//2 |
---|
326 | n/a | x2 = x1+WIDTH |
---|
327 | n/a | y2 = (self.array.maxvalue+1)*YGRID |
---|
328 | n/a | y1 = y2 - (self.value)*YGRID |
---|
329 | n/a | return x1, y1, x2, y2 |
---|
330 | n/a | |
---|
331 | n/a | def nearestindex(self, x): |
---|
332 | n/a | return int(round(float(x)/XGRID)) - 1 |
---|
333 | n/a | |
---|
334 | n/a | |
---|
335 | n/a | # Subroutines that don't need an object |
---|
336 | n/a | |
---|
337 | n/a | def steps(here, there): |
---|
338 | n/a | nsteps = abs(here - there) |
---|
339 | n/a | if nsteps <= 3: |
---|
340 | n/a | nsteps = nsteps * 3 |
---|
341 | n/a | elif nsteps <= 5: |
---|
342 | n/a | nsteps = nsteps * 2 |
---|
343 | n/a | elif nsteps > 10: |
---|
344 | n/a | nsteps = 10 |
---|
345 | n/a | return nsteps |
---|
346 | n/a | |
---|
347 | n/a | def interpolate(oldpts, newpts, n): |
---|
348 | n/a | if len(oldpts) != len(newpts): |
---|
349 | n/a | raise ValueError("can't interpolate arrays of different length") |
---|
350 | n/a | pts = [0]*len(oldpts) |
---|
351 | n/a | res = [tuple(oldpts)] |
---|
352 | n/a | for i in range(1, n): |
---|
353 | n/a | for k in range(len(pts)): |
---|
354 | n/a | pts[k] = oldpts[k] + (newpts[k] - oldpts[k])*i//n |
---|
355 | n/a | res.append(tuple(pts)) |
---|
356 | n/a | res.append(tuple(newpts)) |
---|
357 | n/a | return res |
---|
358 | n/a | |
---|
359 | n/a | |
---|
360 | n/a | # Various (un)sorting algorithms |
---|
361 | n/a | |
---|
362 | n/a | def uniform(array): |
---|
363 | n/a | size = array.getsize() |
---|
364 | n/a | array.setdata([(size+1)//2] * size) |
---|
365 | n/a | array.reset("Uniform data, size %d" % size) |
---|
366 | n/a | |
---|
367 | n/a | def distinct(array): |
---|
368 | n/a | size = array.getsize() |
---|
369 | n/a | array.setdata(range(1, size+1)) |
---|
370 | n/a | array.reset("Distinct data, size %d" % size) |
---|
371 | n/a | |
---|
372 | n/a | def randomize(array): |
---|
373 | n/a | array.reset("Randomizing") |
---|
374 | n/a | n = array.getsize() |
---|
375 | n/a | for i in range(n): |
---|
376 | n/a | j = random.randint(0, n-1) |
---|
377 | n/a | array.swap(i, j) |
---|
378 | n/a | array.message("Randomized") |
---|
379 | n/a | |
---|
380 | n/a | def insertionsort(array): |
---|
381 | n/a | size = array.getsize() |
---|
382 | n/a | array.reset("Insertion sort") |
---|
383 | n/a | for i in range(1, size): |
---|
384 | n/a | j = i-1 |
---|
385 | n/a | while j >= 0: |
---|
386 | n/a | if array.compare(j, j+1) <= 0: |
---|
387 | n/a | break |
---|
388 | n/a | array.swap(j, j+1) |
---|
389 | n/a | j = j-1 |
---|
390 | n/a | array.message("Sorted") |
---|
391 | n/a | |
---|
392 | n/a | def selectionsort(array): |
---|
393 | n/a | size = array.getsize() |
---|
394 | n/a | array.reset("Selection sort") |
---|
395 | n/a | try: |
---|
396 | n/a | for i in range(size): |
---|
397 | n/a | array.show_partition(i, size) |
---|
398 | n/a | for j in range(i+1, size): |
---|
399 | n/a | if array.compare(i, j) > 0: |
---|
400 | n/a | array.swap(i, j) |
---|
401 | n/a | array.message("Sorted") |
---|
402 | n/a | finally: |
---|
403 | n/a | array.hide_partition() |
---|
404 | n/a | |
---|
405 | n/a | def bubblesort(array): |
---|
406 | n/a | size = array.getsize() |
---|
407 | n/a | array.reset("Bubble sort") |
---|
408 | n/a | for i in range(size): |
---|
409 | n/a | for j in range(1, size): |
---|
410 | n/a | if array.compare(j-1, j) > 0: |
---|
411 | n/a | array.swap(j-1, j) |
---|
412 | n/a | array.message("Sorted") |
---|
413 | n/a | |
---|
414 | n/a | def quicksort(array): |
---|
415 | n/a | size = array.getsize() |
---|
416 | n/a | array.reset("Quicksort") |
---|
417 | n/a | try: |
---|
418 | n/a | stack = [(0, size)] |
---|
419 | n/a | while stack: |
---|
420 | n/a | first, last = stack[-1] |
---|
421 | n/a | del stack[-1] |
---|
422 | n/a | array.show_partition(first, last) |
---|
423 | n/a | if last-first < 5: |
---|
424 | n/a | array.message("Insertion sort") |
---|
425 | n/a | for i in range(first+1, last): |
---|
426 | n/a | j = i-1 |
---|
427 | n/a | while j >= first: |
---|
428 | n/a | if array.compare(j, j+1) <= 0: |
---|
429 | n/a | break |
---|
430 | n/a | array.swap(j, j+1) |
---|
431 | n/a | j = j-1 |
---|
432 | n/a | continue |
---|
433 | n/a | array.message("Choosing pivot") |
---|
434 | n/a | j, i, k = first, (first+last) // 2, last-1 |
---|
435 | n/a | if array.compare(k, i) < 0: |
---|
436 | n/a | array.swap(k, i) |
---|
437 | n/a | if array.compare(k, j) < 0: |
---|
438 | n/a | array.swap(k, j) |
---|
439 | n/a | if array.compare(j, i) < 0: |
---|
440 | n/a | array.swap(j, i) |
---|
441 | n/a | pivot = j |
---|
442 | n/a | array.show_pivot(pivot) |
---|
443 | n/a | array.message("Pivot at left of partition") |
---|
444 | n/a | array.wait(1000) |
---|
445 | n/a | left = first |
---|
446 | n/a | right = last |
---|
447 | n/a | while 1: |
---|
448 | n/a | array.message("Sweep right pointer") |
---|
449 | n/a | right = right-1 |
---|
450 | n/a | array.show_right(right) |
---|
451 | n/a | while right > first and array.compare(right, pivot) >= 0: |
---|
452 | n/a | right = right-1 |
---|
453 | n/a | array.show_right(right) |
---|
454 | n/a | array.message("Sweep left pointer") |
---|
455 | n/a | left = left+1 |
---|
456 | n/a | array.show_left(left) |
---|
457 | n/a | while left < last and array.compare(left, pivot) <= 0: |
---|
458 | n/a | left = left+1 |
---|
459 | n/a | array.show_left(left) |
---|
460 | n/a | if left > right: |
---|
461 | n/a | array.message("End of partition") |
---|
462 | n/a | break |
---|
463 | n/a | array.message("Swap items") |
---|
464 | n/a | array.swap(left, right) |
---|
465 | n/a | array.message("Swap pivot back") |
---|
466 | n/a | array.swap(pivot, right) |
---|
467 | n/a | n1 = right-first |
---|
468 | n/a | n2 = last-left |
---|
469 | n/a | if n1 > 1: stack.append((first, right)) |
---|
470 | n/a | if n2 > 1: stack.append((left, last)) |
---|
471 | n/a | array.message("Sorted") |
---|
472 | n/a | finally: |
---|
473 | n/a | array.hide_partition() |
---|
474 | n/a | |
---|
475 | n/a | def demosort(array): |
---|
476 | n/a | while 1: |
---|
477 | n/a | for alg in [quicksort, insertionsort, selectionsort, bubblesort]: |
---|
478 | n/a | randomize(array) |
---|
479 | n/a | alg(array) |
---|
480 | n/a | |
---|
481 | n/a | |
---|
482 | n/a | # Sort demo class -- usable as a Grail applet |
---|
483 | n/a | |
---|
484 | n/a | class SortDemo: |
---|
485 | n/a | |
---|
486 | n/a | def __init__(self, master, size=15): |
---|
487 | n/a | self.master = master |
---|
488 | n/a | self.size = size |
---|
489 | n/a | self.busy = 0 |
---|
490 | n/a | self.array = Array(self.master) |
---|
491 | n/a | |
---|
492 | n/a | self.botframe = Frame(master) |
---|
493 | n/a | self.botframe.pack(side=BOTTOM) |
---|
494 | n/a | self.botleftframe = Frame(self.botframe) |
---|
495 | n/a | self.botleftframe.pack(side=LEFT, fill=Y) |
---|
496 | n/a | self.botrightframe = Frame(self.botframe) |
---|
497 | n/a | self.botrightframe.pack(side=RIGHT, fill=Y) |
---|
498 | n/a | |
---|
499 | n/a | self.b_qsort = Button(self.botleftframe, |
---|
500 | n/a | text="Quicksort", command=self.c_qsort) |
---|
501 | n/a | self.b_qsort.pack(fill=X) |
---|
502 | n/a | self.b_isort = Button(self.botleftframe, |
---|
503 | n/a | text="Insertion sort", command=self.c_isort) |
---|
504 | n/a | self.b_isort.pack(fill=X) |
---|
505 | n/a | self.b_ssort = Button(self.botleftframe, |
---|
506 | n/a | text="Selection sort", command=self.c_ssort) |
---|
507 | n/a | self.b_ssort.pack(fill=X) |
---|
508 | n/a | self.b_bsort = Button(self.botleftframe, |
---|
509 | n/a | text="Bubble sort", command=self.c_bsort) |
---|
510 | n/a | self.b_bsort.pack(fill=X) |
---|
511 | n/a | |
---|
512 | n/a | # Terrible hack to overcome limitation of OptionMenu... |
---|
513 | n/a | class MyIntVar(IntVar): |
---|
514 | n/a | def __init__(self, master, demo): |
---|
515 | n/a | self.demo = demo |
---|
516 | n/a | IntVar.__init__(self, master) |
---|
517 | n/a | def set(self, value): |
---|
518 | n/a | IntVar.set(self, value) |
---|
519 | n/a | if str(value) != '0': |
---|
520 | n/a | self.demo.resize(value) |
---|
521 | n/a | |
---|
522 | n/a | self.v_size = MyIntVar(self.master, self) |
---|
523 | n/a | self.v_size.set(size) |
---|
524 | n/a | sizes = [1, 2, 3, 4] + list(range(5, 55, 5)) |
---|
525 | n/a | if self.size not in sizes: |
---|
526 | n/a | sizes.append(self.size) |
---|
527 | n/a | sizes.sort() |
---|
528 | n/a | self.m_size = OptionMenu(self.botleftframe, self.v_size, *sizes) |
---|
529 | n/a | self.m_size.pack(fill=X) |
---|
530 | n/a | |
---|
531 | n/a | self.v_speed = StringVar(self.master) |
---|
532 | n/a | self.v_speed.set("normal") |
---|
533 | n/a | self.m_speed = OptionMenu(self.botleftframe, self.v_speed, |
---|
534 | n/a | "single-step", "normal", "fast", "fastest") |
---|
535 | n/a | self.m_speed.pack(fill=X) |
---|
536 | n/a | |
---|
537 | n/a | self.b_step = Button(self.botleftframe, |
---|
538 | n/a | text="Step", command=self.c_step) |
---|
539 | n/a | self.b_step.pack(fill=X) |
---|
540 | n/a | |
---|
541 | n/a | self.b_randomize = Button(self.botrightframe, |
---|
542 | n/a | text="Randomize", command=self.c_randomize) |
---|
543 | n/a | self.b_randomize.pack(fill=X) |
---|
544 | n/a | self.b_uniform = Button(self.botrightframe, |
---|
545 | n/a | text="Uniform", command=self.c_uniform) |
---|
546 | n/a | self.b_uniform.pack(fill=X) |
---|
547 | n/a | self.b_distinct = Button(self.botrightframe, |
---|
548 | n/a | text="Distinct", command=self.c_distinct) |
---|
549 | n/a | self.b_distinct.pack(fill=X) |
---|
550 | n/a | self.b_demo = Button(self.botrightframe, |
---|
551 | n/a | text="Demo", command=self.c_demo) |
---|
552 | n/a | self.b_demo.pack(fill=X) |
---|
553 | n/a | self.b_cancel = Button(self.botrightframe, |
---|
554 | n/a | text="Cancel", command=self.c_cancel) |
---|
555 | n/a | self.b_cancel.pack(fill=X) |
---|
556 | n/a | self.b_cancel.config(state=DISABLED) |
---|
557 | n/a | self.b_quit = Button(self.botrightframe, |
---|
558 | n/a | text="Quit", command=self.c_quit) |
---|
559 | n/a | self.b_quit.pack(fill=X) |
---|
560 | n/a | |
---|
561 | n/a | def resize(self, newsize): |
---|
562 | n/a | if self.busy: |
---|
563 | n/a | self.master.bell() |
---|
564 | n/a | return |
---|
565 | n/a | self.size = newsize |
---|
566 | n/a | self.array.setdata(range(1, self.size+1)) |
---|
567 | n/a | |
---|
568 | n/a | def c_qsort(self): |
---|
569 | n/a | self.run(quicksort) |
---|
570 | n/a | |
---|
571 | n/a | def c_isort(self): |
---|
572 | n/a | self.run(insertionsort) |
---|
573 | n/a | |
---|
574 | n/a | def c_ssort(self): |
---|
575 | n/a | self.run(selectionsort) |
---|
576 | n/a | |
---|
577 | n/a | def c_bsort(self): |
---|
578 | n/a | self.run(bubblesort) |
---|
579 | n/a | |
---|
580 | n/a | def c_demo(self): |
---|
581 | n/a | self.run(demosort) |
---|
582 | n/a | |
---|
583 | n/a | def c_randomize(self): |
---|
584 | n/a | self.run(randomize) |
---|
585 | n/a | |
---|
586 | n/a | def c_uniform(self): |
---|
587 | n/a | self.run(uniform) |
---|
588 | n/a | |
---|
589 | n/a | def c_distinct(self): |
---|
590 | n/a | self.run(distinct) |
---|
591 | n/a | |
---|
592 | n/a | def run(self, func): |
---|
593 | n/a | if self.busy: |
---|
594 | n/a | self.master.bell() |
---|
595 | n/a | return |
---|
596 | n/a | self.busy = 1 |
---|
597 | n/a | self.array.setspeed(self.v_speed.get()) |
---|
598 | n/a | self.b_cancel.config(state=NORMAL) |
---|
599 | n/a | try: |
---|
600 | n/a | func(self.array) |
---|
601 | n/a | except Array.Cancelled: |
---|
602 | n/a | pass |
---|
603 | n/a | self.b_cancel.config(state=DISABLED) |
---|
604 | n/a | self.busy = 0 |
---|
605 | n/a | |
---|
606 | n/a | def c_cancel(self): |
---|
607 | n/a | if not self.busy: |
---|
608 | n/a | self.master.bell() |
---|
609 | n/a | return |
---|
610 | n/a | self.array.cancel() |
---|
611 | n/a | |
---|
612 | n/a | def c_step(self): |
---|
613 | n/a | if not self.busy: |
---|
614 | n/a | self.master.bell() |
---|
615 | n/a | return |
---|
616 | n/a | self.v_speed.set("single-step") |
---|
617 | n/a | self.array.setspeed("single-step") |
---|
618 | n/a | self.array.step() |
---|
619 | n/a | |
---|
620 | n/a | def c_quit(self): |
---|
621 | n/a | if self.busy: |
---|
622 | n/a | self.array.cancel() |
---|
623 | n/a | self.master.after_idle(self.master.quit) |
---|
624 | n/a | |
---|
625 | n/a | |
---|
626 | n/a | # Main program -- for stand-alone operation outside Grail |
---|
627 | n/a | |
---|
628 | n/a | def main(): |
---|
629 | n/a | root = Tk() |
---|
630 | n/a | demo = SortDemo(root) |
---|
631 | n/a | root.protocol('WM_DELETE_WINDOW', demo.c_quit) |
---|
632 | n/a | root.mainloop() |
---|
633 | n/a | |
---|
634 | n/a | if __name__ == '__main__': |
---|
635 | n/a | main() |
---|