1
1
from tkinter import *
2
- from tkinter import ttk
2
+ from tkinter import ttk , messagebox
3
3
from ttkbootstrap import *
4
4
import numpy as np
5
5
import time
@@ -28,20 +28,21 @@ def __init__(self, root, title) -> None:
28
28
self .ss .grid (column = 2 , row = 1 , padx = 5 , pady = 5 )
29
29
self .ms = ttk .Button (self .root , text = 'Merge Sort' , style = 'info.TButton' , padding = 5 , width = 15 ,
30
30
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 )
33
32
self .qs = ttk .Button (self .root , text = 'Quick Sort' , style = 'info.TButton' , padding = 5 , width = 15 ,
34
33
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 )
37
35
self .start = ttk .Button (self .root , text = 'Start' , padding = 5 , width = 15 ,
38
36
command = self .start )
39
37
self .start .grid (column = 5 , row = 2 , padx = 5 , pady = 5 )
40
38
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 )
45
46
46
47
self .shuf = ttk .Button (self .root , text = 'Shuffle' , style = 'info.Outline.TButton' , padding = 5 , width = 15 ,
47
48
command = self .shuffle )
@@ -54,14 +55,14 @@ def __init__(self, root, title) -> None:
54
55
55
56
56
57
57
-
58
58
# some constants
59
+ self .speed = 0.2
59
60
self .N = 30
60
61
self .colours = ['dodgerblue' for i in range (self .N )]
61
- N = self . N
62
+ N = 30
62
63
self .data = np .linspace (5 ,400 ,N ,dtype = np .uint16 )
63
64
np .random .shuffle (self .data )
64
- self .display (self . N ,self .data ,self .colours )
65
+ self .display (N ,self .data ,self .colours )
65
66
66
67
67
68
def display (self ,N : int ,a : list ,rong : list ):
@@ -71,14 +72,30 @@ def display(self,N: int,a: list,rong: list):
71
72
rong = array of colours of each and every rectangle'''
72
73
73
74
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
+
76
78
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 ])
78
80
79
81
self .root .update_idletasks ()
80
82
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
+
82
99
''' bubble sort'''
83
100
def bubble (self ):
84
101
if self .st ['bubble' ] is False :
@@ -93,11 +110,11 @@ def bubble(self):
93
110
self .ms .config (style = 'info.TButton' )
94
111
self .ss .config (style = 'info.TButton' )
95
112
self .Is .config (style = 'info.TButton' )
96
- # print(self.st)
113
+
97
114
else :
98
115
self .st ['bubble' ] = False
99
116
self .bs .config (style = 'info.TButton' )
100
- # print(self.st)
117
+
101
118
102
119
''' merge sort'''
103
120
def merge (self ):
@@ -113,11 +130,11 @@ def merge(self):
113
130
self .bs .config (style = 'info.TButton' )
114
131
self .ss .config (style = 'info.TButton' )
115
132
self .Is .config (style = 'info.TButton' )
116
- # print(self.st)
133
+
117
134
else :
118
135
self .st ['merge' ] = False
119
136
self .ms .config (style = 'info.TButton' )
120
- # print(self.st)
137
+
121
138
122
139
''' quick sort'''
123
140
def quick (self ):
@@ -133,11 +150,11 @@ def quick(self):
133
150
self .bs .config (style = 'info.TButton' )
134
151
self .ss .config (style = 'info.TButton' )
135
152
self .Is .config (style = 'info.TButton' )
136
- # print(self.st)
153
+
137
154
else :
138
155
self .st ['quick' ] = False
139
156
self .qs .config (style = 'info.TButton' )
140
- # print(self.st)
157
+
141
158
142
159
''' selection sort'''
143
160
def selection (self ):
@@ -153,11 +170,11 @@ def selection(self):
153
170
self .bs .config (style = 'info.TButton' )
154
171
self .ms .config (style = 'info.TButton' )
155
172
self .Is .config (style = 'info.TButton' )
156
- # print(self.st)
173
+
157
174
else :
158
175
self .st ['selection' ] = False
159
176
self .ss .config (style = 'info.TButton' )
160
- # print(self.st)
177
+
161
178
162
179
''' insertion sort'''
163
180
def insertion (self ):
@@ -173,27 +190,20 @@ def insertion(self):
173
190
self .bs .config (style = 'info.TButton' )
174
191
self .ss .config (style = 'info.TButton' )
175
192
self .ms .config (style = 'info.TButton' )
176
- # print(self.st)
193
+
177
194
else :
178
195
self .st ['insertion' ] = False
179
196
self .Is .config (style = 'info.TButton' )
180
- # print(self.st)
197
+
181
198
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 ):
190
200
if self .st ['bubble' ] is True :
191
201
for i in range (self .N - 1 ):
192
202
for j in range (self .N - 1 - i ):
193
203
if self .data [j ]> self .data [j + 1 ]:
194
204
self .data [j ],self .data [j + 1 ]= self .data [j + 1 ],self .data [j ]
195
205
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 )
197
207
self .display (self .N ,self .data ,['green' for _ in range (self .N )])
198
208
199
209
elif self .st ['insertion' ] is True :
@@ -204,7 +214,7 @@ def start(self,T=0.2):
204
214
self .data [i + 1 ]= self .data [i ]
205
215
i -= 1
206
216
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 )
208
218
self .data [i + 1 ]= key
209
219
self .display (self .N ,self .data ,['green' for _ in range (self .N )])
210
220
@@ -213,9 +223,9 @@ def start(self,T=0.2):
213
223
min_index = i
214
224
# loop to find the minimum element and its index
215
225
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 )
216
228
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 )
219
229
min_index = j
220
230
if min_index != i :
221
231
self .data [i ], self .data [min_index ]= self .data [min_index ],self .data [i ]
@@ -231,8 +241,9 @@ def start(self,T=0.2):
231
241
self .display (self .N ,self .data ,['green' for _ in range (self .N )])
232
242
233
243
else :
234
- '''show messege box'''
235
- pass
244
+ #show messege box
245
+ messagebox .showerror ("Algorithm Visualizer" , "You didn't select any sorting algorithm" )
246
+
236
247
237
248
# -----------merge sort-------------------------------------
238
249
@@ -259,7 +270,7 @@ def mergesort(self,a,front,last):
259
270
while i != front :
260
271
a [i ]= a [i - 1 ]
261
272
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 )
263
274
i -= 1
264
275
a [front ]= temp
265
276
@@ -268,7 +279,7 @@ def mergesort(self,a,front,last):
268
279
rj += 1
269
280
270
281
self .display (self .N ,self .data ,['dodgerblue' for _ in range (self .N )])
271
- time .sleep (0.2 )
282
+ time .sleep (self . speed )
272
283
273
284
#--------------------------------------------------quick sort---------------
274
285
0 commit comments