Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e1b3745

Browse files
committed
321654
1 parent 58c6449 commit e1b3745

File tree

1 file changed

+29
-53
lines changed

1 file changed

+29
-53
lines changed

‎algo_visualizer.py

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class window:
99

1010
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
1212

1313
def __init__(self, root, title) -> None:
1414
self.root = root
@@ -29,31 +29,35 @@ def __init__(self, root, title) -> None:
2929
self.ms = ttk.Button(self.root, text='Merge Sort', style='info.TButton', padding=5, width=15,
3030
command=self.merge)
3131
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+
3533
self.qs = ttk.Button(self.root, text='Quick Sort', style='info.TButton', padding=5, width=15,
3634
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)
3836

3937
self.start = ttk.Button(self.root, text='Start', padding=5, width=15,
4038
command=self.start)
4139
self.start.grid(column=5, row=2, padx=5, pady=5)
4240

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)
4344

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,
4647
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)
4849

4950
# 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')
5253
self.canvas.grid(row=4, padx=5, pady=10, columnspan=6)
5354

55+
56+
57+
5458
# some constants
5559
self.N=30
56-
self.colours=['blue' for i in range(self.N)]
60+
self.colours=['dodgerblue' for i in range(self.N)]
5761
N=self.N
5862
self.data=np.linspace(5,400,N,dtype=np.uint16)
5963
np.random.shuffle(self.data)
@@ -85,8 +89,7 @@ def bubble(self):
8589
if i != 'bubble':
8690
self.st[i]=False
8791

88-
self.qs.config(style='info.TButton')
89-
self.buckets.config(style='info.TButton')
92+
self.qs.config(style='info.TButton')
9093
self.ms.config(style='info.TButton')
9194
self.ss.config(style='info.TButton')
9295
self.Is.config(style='info.TButton')
@@ -106,8 +109,7 @@ def merge(self):
106109
if i != 'merge':
107110
self.st[i]=False
108111

109-
self.qs.config(style='info.TButton')
110-
self.buckets.config(style='info.TButton')
112+
self.qs.config(style='info.TButton')
111113
self.bs.config(style='info.TButton')
112114
self.ss.config(style='info.TButton')
113115
self.Is.config(style='info.TButton')
@@ -127,8 +129,7 @@ def quick(self):
127129
if i != 'quick':
128130
self.st[i]=False
129131

130-
self.ms.config(style='info.TButton')
131-
self.buckets.config(style='info.TButton')
132+
self.ms.config(style='info.TButton')
132133
self.bs.config(style='info.TButton')
133134
self.ss.config(style='info.TButton')
134135
self.Is.config(style='info.TButton')
@@ -148,8 +149,7 @@ def selection(self):
148149
if i != 'selection':
149150
self.st[i]=False
150151

151-
self.qs.config(style='info.TButton')
152-
self.buckets.config(style='info.TButton')
152+
self.qs.config(style='info.TButton')
153153
self.bs.config(style='info.TButton')
154154
self.ms.config(style='info.TButton')
155155
self.Is.config(style='info.TButton')
@@ -169,8 +169,7 @@ def insertion(self):
169169
for i in self.st:
170170
if i != 'insertion':
171171
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')
174173
self.bs.config(style='info.TButton')
175174
self.ss.config(style='info.TButton')
176175
self.ms.config(style='info.TButton')
@@ -180,27 +179,7 @@ def insertion(self):
180179
self.Is.config(style='info.TButton')
181180
# print(self.st)
182181

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+
204183
def shuffle(self):
205184
self.canvas.delete('all')
206185
np.random.shuffle(self.data)
@@ -213,7 +192,7 @@ def start(self,T=0.2):
213192
for j in range(self.N-1-i):
214193
if self.data[j]>self.data[j+1]:
215194
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)])
217196
time.sleep(T)
218197
self.display(self.N,self.data,['green' for _ in range(self.N)])
219198

@@ -224,7 +203,7 @@ def start(self,T=0.2):
224203
while i>=0 and self.data[i]>key:
225204
self.data[i+1]=self.data[i]
226205
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)])
228207
time.sleep(T)
229208
self.data[i+1]=key
230209
self.display(self.N,self.data,['green' for _ in range(self.N)])
@@ -235,7 +214,7 @@ def start(self,T=0.2):
235214
# loop to find the minimum element and its index
236215
for j in range(i+1,len(self.data)):
237216
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)])
239218
time.sleep(T)
240219
min_index=j
241220
if min_index!=i:
@@ -251,9 +230,6 @@ def start(self,T=0.2):
251230
self.quicksort(self.data,0,self.N-1)
252231
self.display(self.N,self.data,['green' for _ in range(self.N)])
253232

254-
elif self.st['bucket'] is True:
255-
pass
256-
257233
else:
258234
'''show messege box'''
259235
pass
@@ -268,7 +244,7 @@ def mergesort(self,a,front,last):
268244
self.mergesort(a,mid+1,last)
269245

270246

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)])
272248

273249
rj=mid+1
274250
if a[mid]<=a[mid+1]:
@@ -282,7 +258,7 @@ def mergesort(self,a,front,last):
282258
i=rj
283259
while i!=front:
284260
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)])
286262
time.sleep(0.1)
287263
i-=1
288264
a[front]=temp
@@ -291,7 +267,7 @@ def mergesort(self,a,front,last):
291267
mid+=1
292268
rj+=1
293269

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)])
295271
time.sleep(0.2)
296272

297273
#--------------------------------------------------quick sort---------------
@@ -323,11 +299,11 @@ def quicksort(self,a,i,j):
323299

324300
self.quicksort(a,i,x-1)
325301
self.quicksort(a,x+1,j)
326-
#--------------------------------------------------quick sort---------------
302+
#--------------------------------------------------
327303

328304

329305

330-
win = Style(theme='cyborg').master
306+
win = Style(theme='darkly').master
331307
obj = window(win, 'Sorting Algorithm Visualizer')
332308

333309
win.mainloop()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /