-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphical_sort.py
executable file
·280 lines (237 loc) · 8.14 KB
/
graphical_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
#!/usr/bin/env python3
from random import randint
from collections import namedtuple
import pygame
from pygame import Rect
import ptext
import pgzrun
from threading import Event, Thread, Lock
draw_event = Event()
DATA_SIZE=50
FPS=24
DataPoint = namedtuple('DataPoint',['value','color'])
class SortObject(Thread):
name = "Unknown"
def __init__(self, starting_data, surface):
super().__init__()
self._data = starting_data[:]
self._sorted = False
self._surface = surface
self._status = {}
self._hilights = []
self.drawing_ready = Lock()
self._stop_event = Event()
def stop(self):
self._stop_event.set()
def _should_stop(self):
return self._stop_event.is_set()
@staticmethod
def _swap(x,y):
return y,x
def _draw_frame(self):
self.drawing_ready.acquire()
bar_width = self._surface.get_width() / len(self._data)
self._surface.fill(pygame.Color('lightblue'))
for hilight in self._hilights:
r = Rect(hilight * bar_width, 0, bar_width, self._surface.get_height())
pygame.draw.rect(self._surface, pygame.Color('red'), r, 0)
# pygame.draw.rect(self._surface, pygame.Color('black'), r, 1)
for index, item in enumerate(self._data):
r = Rect((index * bar_width) + 1, self._surface.get_height() - item.value, bar_width - 2, item.value )
pygame.draw.rect(self._surface, pygame.Color(item.color), r, 0)
pygame.draw.rect(self._surface, pygame.Color('black'), r, 1)
if self.is_sorted:
text_color = 'green'
else:
text_color = 'orange'
ptext.draw(self.name, (10, 10), surf=self._surface, color=text_color, owidth=1, ocolor = 'black')
self.drawing_ready.release()
draw_event.wait()
@property
def surface(self):
return self._surface
def _swap_indices(self, x, y):
v1, v2 = self._swap(self._data[x], self._data[y])
self._data[x], self._data[y] = v1, v2
@property
def is_sorted(self):
return self._sorted
def do_sort(self):
raise(NotImplementedError)
def run(self):
self._draw_frame() # Draw the first frame
self.do_sort()
class BubbleSort(SortObject):
name = "BubbleSort"
def do_sort(self):
while not self.is_sorted:
done_sorting = True
for sort_pointer in range(len(self._data) - 1):
if self._should_stop():
return
self._hilights = (sort_pointer, sort_pointer + 1)
self._draw_frame()
if self._data[sort_pointer].value > self._data[sort_pointer + 1].value:
self._swap_indices(sort_pointer, sort_pointer + 1)
self._draw_frame()
done_sorting = False
self._sorted = done_sorting
# Draw the final sorted frame
self._hilights = []
self._draw_frame()
class OptimizedBubbleSort(SortObject):
name = "OptimizedBubbleSort"
def do_sort(self):
max_sort = len(self._data)
while not self.is_sorted:
done_sorting = True
for sort_pointer in range(max_sort - 1):
self._hilights = (sort_pointer, sort_pointer + 1)
self._draw_frame()
if self._data[sort_pointer].value > self._data[sort_pointer + 1].value:
self._swap_indices(sort_pointer, sort_pointer + 1)
self._draw_frame()
done_sorting = False
max_sort -= 1
self._sorted = done_sorting
# Draw the final sorted frame
self._hilights = []
self._draw_frame()
class ShellSort(SortObject):
name = "ShellSort"
def do_sort(self):
gap = len(self._data)
while gap > 1:
gap = gap // 2
for i in range(gap, len(self._data), gap):
temp = self._data[i]
j = i
while j >= gap and self._data[j - gap] > temp:
self._hilights = (j, j - gap)
self._draw_frame()
self._data[j] = self._data[j - gap]
self._draw_frame()
j -= gap
self._draw_frame()
self._data[j] = temp
self._sorted = True
# Draw the final sorted frame
self._hilights = []
self._draw_frame()
class InsertionSort(SortObject):
name = "InsertionSort"
def do_sort(self):
# An insertion sort is a shell sort with gap = 1
gap = 2
while gap > 1:
gap = gap // 2
for i in range(gap, len(self._data), gap):
temp = self._data[i]
j = i
while j >= gap and self._data[j - gap] > temp:
self._hilights = (j, j - gap)
self._draw_frame()
self._data[j] = self._data[j - gap]
self._draw_frame()
j -= gap
self._draw_frame()
self._data[j] = temp
self._sorted = True
# Draw the final sorted frame
self._hilights = []
self._draw_frame()
class QuickSort(SortObject):
name = "QuickSort"
def __partition(self, lo, hi):
pivot = self._data[hi]
i = lo
for j in range(lo, hi):
self._hilights = (j, hi)
self._draw_frame()
if self._data[j] < pivot:
self._hilights = (i,j)
self._draw_frame()
self._swap_indices(i,j)
self._draw_frame()
i = i + 1
self._hilights = (i,hi)
self._draw_frame()
self._swap_indices(i, hi)
self._draw_frame()
return i
def __quicksort(self, lo, hi):
if lo < hi:
p = self.__partition(lo, hi)
self.__quicksort(lo, p - 1)
self.__quicksort(p + 1, hi)
def do_sort(self):
self.__quicksort(0, len(self._data) - 1)
self._sorted = True
# Draw the final sorted frame
self._hilights = []
self._draw_frame()
class SelectionSort(SortObject):
name = "SelectionSort"
def do_sort(self):
for j in range(len(self._data) - 1):
iMin = j
for i in range(j + 1, len(self._data)):
self._hilights = (i,iMin)
self._draw_frame()
if self._data[i] < self._data[iMin]:
iMin = i
if iMin != j:
self._hilights = (j, iMin)
self._draw_frame()
self._swap_indices(j, iMin)
self._draw_frame()
# Draw the final sorted frame
self._sorted = True
self._hilights = []
self._draw_frame()
data = [DataPoint(randint(2,100), "#%06X" % randint(0, (2**24) - 1)) for x in range(DATA_SIZE)]
sorter_types = [
BubbleSort,
OptimizedBubbleSort,
ShellSort,
InsertionSort,
QuickSort,
SelectionSort,
]
BOX_WIDTH = 400
BOX_HEIGHT = 100
Sorter = namedtuple('Sorter', ['name','sorter'])
WIDTH = BOX_WIDTH + 10
HEIGHT = ((BOX_HEIGHT + 5) * len(sorter_types)) + 5
sorters = None
def draw():
global time_accumlate
if time_accumlate >= 1.0 / FPS:
time_accumlate = 0
screen.fill('grey')
for index, s in enumerate(sorters):
s.sorter.drawing_ready.acquire()
screen.blit(s.sorter.surface, (5, 5 + (index * (BOX_HEIGHT + 5))))
s.sorter.drawing_ready.release()
draw_event.set()
draw_event.clear()
time_accumlate = 1.0
def update(dt):
global time_accumlate
time_accumlate += dt
global sorters
if sorters is None:
# This is the first time through, but we have to do this here so the
# video mode has been set
sorters = []
for s in sorter_types:
sorters.append(
Sorter(name = s.name,
sorter = s(data,
pygame.Surface((BOX_WIDTH, BOX_HEIGHT))
)
)
)
sorters[-1].sorter.start()
if __name__ == "__main__":
pgzrun.go()