8
8
class window :
9
9
10
10
st = {'bubble' : False , 'insertion' : False , 'selection' : False ,
11
- 'merge' : False , 'bucket' : False , ' quick' : False } # to select the sorts
11
+ 'merge' : False , 'quick' : False } # to select the sorts
12
12
13
13
def __init__ (self , root , title ) -> None :
14
14
self .root = root
@@ -29,31 +29,35 @@ def __init__(self, root, title) -> None:
29
29
self .ms = ttk .Button (self .root , text = 'Merge Sort' , style = 'info.TButton' , padding = 5 , width = 15 ,
30
30
command = self .merge )
31
31
self .ms .grid (column = 3 , row = 1 , padx = 5 , pady = 5 )
32
- self .buckets = ttk .Button (self .root , text = 'Bucket Sort' , style = 'info.TButton' , padding = 5 , width = 15 ,
33
- command = self .bucket )
34
- self .buckets .grid (column = 4 , row = 1 , padx = 5 , pady = 5 )
32
+
35
33
self .qs = ttk .Button (self .root , text = 'Quick Sort' , style = 'info.TButton' , padding = 5 , width = 15 ,
36
34
command = self .quick )
37
- self .qs .grid (column = 5 , row = 1 , padx = 5 , pady = 5 )
35
+ self .qs .grid (column = 4 , row = 1 , padx = 5 , pady = 5 )
38
36
39
37
self .start = ttk .Button (self .root , text = 'Start' , padding = 5 , width = 15 ,
40
38
command = self .start )
41
39
self .start .grid (column = 5 , row = 2 , padx = 5 , pady = 5 )
42
40
41
+ self .timespan = ttk .Scale (self .root ,from_ = 0.02 ,to = 0.9 ,value = 0.1 ,style = 'success.Horizontal.TScale' )
42
+ self .timespan .grid (row = 2 ,column = 1 )
43
+ self .arraysize = ttk .Scale (self .root ,from_ = 5 ,to = 100 )
43
44
44
-
45
- self .shuf = ttk .Button (self .root , text = 'Shuffle' , style = 'info.TButton' , padding = 5 , width = 15 ,
45
+
46
+ self .shuf = ttk .Button (self .root , text = 'Shuffle' , style = 'info.Outline. TButton' , padding = 5 , width = 15 ,
46
47
command = self .shuffle )
47
- self .shuf .grid (column = 0 , row = 2 , padx = 5 , pady = 5 )
48
+ self .shuf .grid (column = 5 , row = 1 , padx = 5 , pady = 5 )
48
49
49
50
# Canvas
50
- self .canvas = Canvas (self .root , width = 800 - 5 , height = 400 ,
51
- bg = 'white ' )
51
+ self .canvas = Canvas (self .root , width = 800 - 5 , height = 400 ,highlightbackground = "dodgerblue" , highlightthickness = 2 ,
52
+ bg = 'black ' )
52
53
self .canvas .grid (row = 4 , padx = 5 , pady = 10 , columnspan = 6 )
53
54
55
+
56
+
57
+
54
58
# some constants
55
59
self .N = 30
56
- self .colours = ['blue ' for i in range (self .N )]
60
+ self .colours = ['dodgerblue ' for i in range (self .N )]
57
61
N = self .N
58
62
self .data = np .linspace (5 ,400 ,N ,dtype = np .uint16 )
59
63
np .random .shuffle (self .data )
@@ -85,8 +89,7 @@ def bubble(self):
85
89
if i != 'bubble' :
86
90
self .st [i ]= False
87
91
88
- self .qs .config (style = 'info.TButton' )
89
- self .buckets .config (style = 'info.TButton' )
92
+ self .qs .config (style = 'info.TButton' )
90
93
self .ms .config (style = 'info.TButton' )
91
94
self .ss .config (style = 'info.TButton' )
92
95
self .Is .config (style = 'info.TButton' )
@@ -106,8 +109,7 @@ def merge(self):
106
109
if i != 'merge' :
107
110
self .st [i ]= False
108
111
109
- self .qs .config (style = 'info.TButton' )
110
- self .buckets .config (style = 'info.TButton' )
112
+ self .qs .config (style = 'info.TButton' )
111
113
self .bs .config (style = 'info.TButton' )
112
114
self .ss .config (style = 'info.TButton' )
113
115
self .Is .config (style = 'info.TButton' )
@@ -127,8 +129,7 @@ def quick(self):
127
129
if i != 'quick' :
128
130
self .st [i ]= False
129
131
130
- self .ms .config (style = 'info.TButton' )
131
- self .buckets .config (style = 'info.TButton' )
132
+ self .ms .config (style = 'info.TButton' )
132
133
self .bs .config (style = 'info.TButton' )
133
134
self .ss .config (style = 'info.TButton' )
134
135
self .Is .config (style = 'info.TButton' )
@@ -148,8 +149,7 @@ def selection(self):
148
149
if i != 'selection' :
149
150
self .st [i ]= False
150
151
151
- self .qs .config (style = 'info.TButton' )
152
- self .buckets .config (style = 'info.TButton' )
152
+ self .qs .config (style = 'info.TButton' )
153
153
self .bs .config (style = 'info.TButton' )
154
154
self .ms .config (style = 'info.TButton' )
155
155
self .Is .config (style = 'info.TButton' )
@@ -169,8 +169,7 @@ def insertion(self):
169
169
for i in self .st :
170
170
if i != 'insertion' :
171
171
self .st [i ]= False
172
- self .qs .config (style = 'info.TButton' )
173
- self .buckets .config (style = 'info.TButton' )
172
+ self .qs .config (style = 'info.TButton' )
174
173
self .bs .config (style = 'info.TButton' )
175
174
self .ss .config (style = 'info.TButton' )
176
175
self .ms .config (style = 'info.TButton' )
@@ -180,27 +179,7 @@ def insertion(self):
180
179
self .Is .config (style = 'info.TButton' )
181
180
# print(self.st)
182
181
183
- ''' bucket sort'''
184
- def bucket (self ):
185
- if self .st ['bucket' ] is False :
186
- self .st ['bucket' ] = True
187
- self .buckets .config (style = 'success.TButton' )
188
-
189
- for i in self .st :
190
- if i != 'bucket' :
191
- self .st [i ]= False
192
-
193
- self .qs .config (style = 'info.TButton' )
194
- self .Is .config (style = 'info.TButton' )
195
- self .bs .config (style = 'info.TButton' )
196
- self .ss .config (style = 'info.TButton' )
197
- self .ms .config (style = 'info.TButton' )
198
- # print(self.st)
199
- else :
200
- self .st ['bucket' ] = False
201
- self .buckets .config (style = 'info.TButton' )
202
- # print(self.st)
203
-
182
+
204
183
def shuffle (self ):
205
184
self .canvas .delete ('all' )
206
185
np .random .shuffle (self .data )
@@ -213,7 +192,7 @@ def start(self,T=0.2):
213
192
for j in range (self .N - 1 - i ):
214
193
if self .data [j ]> self .data [j + 1 ]:
215
194
self .data [j ],self .data [j + 1 ]= self .data [j + 1 ],self .data [j ]
216
- self .display (self .N ,self .data ,['purple' if a == j or a == j + 1 else 'green' if a > self .N - 1 - i else 'blue ' for a in range (self .N )])
195
+ self .display (self .N ,self .data ,['purple' if a == j or a == j + 1 else 'green' if a > self .N - 1 - i else 'dodgerblue ' for a in range (self .N )])
217
196
time .sleep (T )
218
197
self .display (self .N ,self .data ,['green' for _ in range (self .N )])
219
198
@@ -224,7 +203,7 @@ def start(self,T=0.2):
224
203
while i >= 0 and self .data [i ]> key :
225
204
self .data [i + 1 ]= self .data [i ]
226
205
i -= 1
227
- self .display (self .N ,self .data ,['purple' if a == j or a == j + 1 else 'green' if a <= j else 'blue ' for a in range (self .N )])
206
+ self .display (self .N ,self .data ,['purple' if a == j or a == j + 1 else 'green' if a <= j else 'dodgerblue ' for a in range (self .N )])
228
207
time .sleep (T )
229
208
self .data [i + 1 ]= key
230
209
self .display (self .N ,self .data ,['green' for _ in range (self .N )])
@@ -235,7 +214,7 @@ def start(self,T=0.2):
235
214
# loop to find the minimum element and its index
236
215
for j in range (i + 1 ,len (self .data )):
237
216
if self .data [min_index ]> self .data [j ]:
238
- self .display (self .N ,self .data ,['purple' if a == j else 'green' if a <= i else 'blue ' for a in range (self .N )])
217
+ self .display (self .N ,self .data ,['purple' if a == j else 'green' if a <= i else 'dodgerblue ' for a in range (self .N )])
239
218
time .sleep (T )
240
219
min_index = j
241
220
if min_index != i :
@@ -251,9 +230,6 @@ def start(self,T=0.2):
251
230
self .quicksort (self .data ,0 ,self .N - 1 )
252
231
self .display (self .N ,self .data ,['green' for _ in range (self .N )])
253
232
254
- elif self .st ['bucket' ] is True :
255
- pass
256
-
257
233
else :
258
234
'''show messege box'''
259
235
pass
@@ -268,7 +244,7 @@ def mergesort(self,a,front,last):
268
244
self .mergesort (a ,mid + 1 ,last )
269
245
270
246
271
- self .display (self .N ,self .data ,['blue ' for _ in range (self .N )])
247
+ self .display (self .N ,self .data ,['dodgerblue ' for _ in range (self .N )])
272
248
273
249
rj = mid + 1
274
250
if a [mid ]<= a [mid + 1 ]:
@@ -282,7 +258,7 @@ def mergesort(self,a,front,last):
282
258
i = rj
283
259
while i != front :
284
260
a [i ]= a [i - 1 ]
285
- self .display (self .N ,self .data ,['purple' if x == i else 'blue ' for x in range (self .N )])
261
+ self .display (self .N ,self .data ,['purple' if x == i else 'dodgerblue ' for x in range (self .N )])
286
262
time .sleep (0.1 )
287
263
i -= 1
288
264
a [front ]= temp
@@ -291,7 +267,7 @@ def mergesort(self,a,front,last):
291
267
mid += 1
292
268
rj += 1
293
269
294
- self .display (self .N ,self .data ,['blue ' for _ in range (self .N )])
270
+ self .display (self .N ,self .data ,['dodgerblue ' for _ in range (self .N )])
295
271
time .sleep (0.2 )
296
272
297
273
#--------------------------------------------------quick sort---------------
@@ -323,11 +299,11 @@ def quicksort(self,a,i,j):
323
299
324
300
self .quicksort (a ,i ,x - 1 )
325
301
self .quicksort (a ,x + 1 ,j )
326
- #--------------------------------------------------quick sort---------------
302
+ #--------------------------------------------------
327
303
328
304
329
305
330
- win = Style (theme = 'cyborg ' ).master
306
+ win = Style (theme = 'darkly ' ).master
331
307
obj = window (win , 'Sorting Algorithm Visualizer' )
332
308
333
309
win .mainloop ()
0 commit comments