Skip to content

Commit a6823fa

Browse files
committed
++
1 parent e1b3745 commit a6823fa

File tree

1 file changed

+53
-42
lines changed

1 file changed

+53
-42
lines changed

algo_visualizer.py

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from tkinter import *
2-
from tkinter import ttk
2+
from tkinter import ttk,messagebox
33
from ttkbootstrap import *
44
import numpy as np
55
import time
@@ -28,20 +28,21 @@ def __init__(self, root, title) -> None:
2828
self.ss.grid(column=2, row=1, padx=5, pady=5)
2929
self.ms = ttk.Button(self.root, text='Merge Sort', style='info.TButton', padding=5, width=15,
3030
command=self.merge)
31-
self.ms.grid(column=3, row=1, padx=5, pady=5)
32-
31+
self.ms.grid(column=3, row=1, padx=5, pady=5)
3332
self.qs = ttk.Button(self.root, text='Quick Sort', style='info.TButton', padding=5, width=15,
3433
command=self.quick)
35-
self.qs.grid(column=4, row=1, padx=5, pady=5)
36-
34+
self.qs.grid(column=4, row=1, padx=5, pady=5)
3735
self.start = ttk.Button(self.root, text='Start', padding=5, width=15,
3836
command=self.start)
3937
self.start.grid(column=5, row=2, padx=5, pady=5)
4038

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)
44-
39+
ttk.Label(self.root, text='Speed & Array Size:').grid(row=2,column=0)
40+
self.timespan=ttk.Scale(self.root,from_=1,to=1000,value=0.1,style='success.Horizontal.TScale',length=250,
41+
command=lambda x:self.slide_function())
42+
self.timespan.grid(row=2,column=1,columnspan=2)
43+
self.arraysize=ttk.Scale(self.root,from_=6,to=120,length=250,style='success.Horizontal.TScale',value=30,
44+
command=lambda x:self.slide_function())
45+
self.arraysize.grid(row=2,column=3,columnspan=2)
4546

4647
self.shuf = ttk.Button(self.root, text='Shuffle', style='info.Outline.TButton', padding=5, width=15,
4748
command=self.shuffle)
@@ -54,14 +55,14 @@ def __init__(self, root, title) -> None:
5455

5556

5657

57-
5858
# some constants
59+
self.speed=0.2
5960
self.N=30
6061
self.colours=['dodgerblue' for i in range(self.N)]
61-
N=self.N
62+
N=30
6263
self.data=np.linspace(5,400,N,dtype=np.uint16)
6364
np.random.shuffle(self.data)
64-
self.display(self.N,self.data,self.colours)
65+
self.display(N,self.data,self.colours)
6566

6667

6768
def display(self,N: int,a: list,rong: list):
@@ -71,14 +72,30 @@ def display(self,N: int,a: list,rong: list):
7172
rong = array of colours of each and every rectangle'''
7273

7374
self.canvas.delete('all')
74-
width=(2*(780/N))//3
75-
gap=(780/N)//3
75+
width=(1570)/(3*N-1)
76+
gap=width/2
77+
7678
for i in range(N):
77-
self.canvas.create_rectangle(0+i*width+i*gap,0,0+(i+1)*width+i*gap,a[i],fill=rong[i])
79+
self.canvas.create_rectangle(7+i*width+i*gap,0,7+(i+1)*width+i*gap,a[i],fill=rong[i])
7880

7981
self.root.update_idletasks()
8082

81-
83+
def slide_function(self):
84+
self.N=int(self.arraysize.get())
85+
self.data=np.linspace(5,400,self.N,dtype=np.uint16)
86+
self.speed=1/self.timespan.get()
87+
self.colours=['dodgerblue' for _ in range(self.N)]
88+
self.shuffle()
89+
90+
91+
def shuffle(self):
92+
self.canvas.delete('all')
93+
self.data=np.linspace(5,400,self.N,dtype=np.uint16)
94+
95+
np.random.shuffle(self.data)
96+
self.display(self.N,self.data,self.colours)
97+
98+
8299
''' bubble sort'''
83100
def bubble(self):
84101
if self.st['bubble'] is False:
@@ -93,11 +110,11 @@ def bubble(self):
93110
self.ms.config(style='info.TButton')
94111
self.ss.config(style='info.TButton')
95112
self.Is.config(style='info.TButton')
96-
# print(self.st)
113+
97114
else:
98115
self.st['bubble'] = False
99116
self.bs.config(style='info.TButton')
100-
# print(self.st)
117+
101118

102119
''' merge sort'''
103120
def merge(self):
@@ -113,11 +130,11 @@ def merge(self):
113130
self.bs.config(style='info.TButton')
114131
self.ss.config(style='info.TButton')
115132
self.Is.config(style='info.TButton')
116-
# print(self.st)
133+
117134
else:
118135
self.st['merge'] = False
119136
self.ms.config(style='info.TButton')
120-
# print(self.st)
137+
121138

122139
''' quick sort'''
123140
def quick(self):
@@ -133,11 +150,11 @@ def quick(self):
133150
self.bs.config(style='info.TButton')
134151
self.ss.config(style='info.TButton')
135152
self.Is.config(style='info.TButton')
136-
# print(self.st)
153+
137154
else:
138155
self.st['quick'] = False
139156
self.qs.config(style='info.TButton')
140-
# print(self.st)
157+
141158

142159
''' selection sort'''
143160
def selection(self):
@@ -153,11 +170,11 @@ def selection(self):
153170
self.bs.config(style='info.TButton')
154171
self.ms.config(style='info.TButton')
155172
self.Is.config(style='info.TButton')
156-
# print(self.st)
173+
157174
else:
158175
self.st['selection'] = False
159176
self.ss.config(style='info.TButton')
160-
# print(self.st)
177+
161178

162179
''' insertion sort'''
163180
def insertion(self):
@@ -173,27 +190,20 @@ def insertion(self):
173190
self.bs.config(style='info.TButton')
174191
self.ss.config(style='info.TButton')
175192
self.ms.config(style='info.TButton')
176-
# print(self.st)
193+
177194
else:
178195
self.st['insertion'] = False
179196
self.Is.config(style='info.TButton')
180-
# print(self.st)
197+
181198

182-
183-
def shuffle(self):
184-
self.canvas.delete('all')
185-
np.random.shuffle(self.data)
186-
self.display(self.N,self.data,self.colours)
187-
# print(self.data)
188-
189-
def start(self,T=0.2):
199+
def start(self):
190200
if self.st['bubble'] is True:
191201
for i in range(self.N-1):
192202
for j in range(self.N-1-i):
193203
if self.data[j]>self.data[j+1]:
194204
self.data[j],self.data[j+1]=self.data[j+1],self.data[j]
195205
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)])
196-
time.sleep(T)
206+
time.sleep(self.speed)
197207
self.display(self.N,self.data,['green' for _ in range(self.N)])
198208

199209
elif self.st['insertion'] is True:
@@ -204,7 +214,7 @@ def start(self,T=0.2):
204214
self.data[i+1]=self.data[i]
205215
i-=1
206216
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)])
207-
time.sleep(T)
217+
time.sleep(self.speed)
208218
self.data[i+1]=key
209219
self.display(self.N,self.data,['green' for _ in range(self.N)])
210220

@@ -213,9 +223,9 @@ def start(self,T=0.2):
213223
min_index=i
214224
# loop to find the minimum element and its index
215225
for j in range(i+1,len(self.data)):
226+
self.display(self.N,self.data,['purple' if a==min_index else 'green' if a<=i else 'dodgerblue' for a in range(self.N)])
227+
time.sleep(self.speed)
216228
if self.data[min_index]>self.data[j]:
217-
self.display(self.N,self.data,['purple' if a==j else 'green' if a<=i else 'dodgerblue' for a in range(self.N)])
218-
time.sleep(T)
219229
min_index=j
220230
if min_index!=i:
221231
self.data[i], self.data[min_index]=self.data[min_index],self.data[i]
@@ -231,8 +241,9 @@ def start(self,T=0.2):
231241
self.display(self.N,self.data,['green' for _ in range(self.N)])
232242

233243
else:
234-
'''show messege box'''
235-
pass
244+
#show messege box
245+
messagebox.showerror("Algorithm Visualizer", "You didn't select any sorting algorithm")
246+
236247

237248
# -----------merge sort-------------------------------------
238249

@@ -259,7 +270,7 @@ def mergesort(self,a,front,last):
259270
while i!=front:
260271
a[i]=a[i-1]
261272
self.display(self.N,self.data,['purple' if x==i else 'dodgerblue' for x in range(self.N)])
262-
time.sleep(0.1)
273+
time.sleep(self.speed)
263274
i-=1
264275
a[front]=temp
265276

@@ -268,7 +279,7 @@ def mergesort(self,a,front,last):
268279
rj+=1
269280

270281
self.display(self.N,self.data,['dodgerblue' for _ in range(self.N)])
271-
time.sleep(0.2)
282+
time.sleep(self.speed)
272283

273284
#--------------------------------------------------quick sort---------------
274285

0 commit comments

Comments
 (0)