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 701bf09

Browse files
committed
first commit
0 parents commit 701bf09

File tree

3 files changed

+286
-0
lines changed

3 files changed

+286
-0
lines changed

‎Licence.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright © 2021 Rajarshi Banerjee
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎Readme.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Sorting Algorithm Visualizer
2+
3+
A gui application to visualize various sorting algorithms using pure python.
4+
5+
### Language : Python 3
6+
7+
### Libraries required
8+
- Tkinter
9+
- ttkbootstrap
10+
- numpy
11+
12+
### Sorting algorithms
13+
- Bubble Sort
14+
- Insertion Sort
15+
- Selection Sort
16+
- Merge Sort
17+
- Quick Sort
18+
- Bucket Sort
19+

‎algo_visualizer.py

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
from tkinter import *
2+
from tkinter import ttk
3+
from ttkbootstrap import *
4+
import numpy as np
5+
import time
6+
7+
8+
class window:
9+
10+
st = {'bubble': False, 'insertion': False, 'selection': False,
11+
'merge': False, 'bucket': False, 'quick': False} # to select the sorts
12+
13+
def __init__(self, root, title) -> None:
14+
self.root = root
15+
self.root.title(title)
16+
self.root.resizable(width=False, height=False)
17+
Label(self.root, text='Sorting Algorithm Visualizer').grid(
18+
row=0, columnspan=6)
19+
# Buttons
20+
self.bs = ttk.Button(self.root, text='Bubble Sort', style='info.TButton', padding=5, width=15,
21+
command=self.bubble)
22+
self.bs.grid(column=0, row=1, padx=5, pady=5)
23+
self.Is = ttk.Button(self.root, text='Insertion Sort', style='info.TButton', padding=5, width=15,
24+
command=self.insertion)
25+
self.Is.grid(column=1, row=1, padx=5, pady=5)
26+
self.ss = ttk.Button(self.root, text='Selection Sort', style='info.TButton', padding=5, width=15,
27+
command=self.selection)
28+
self.ss.grid(column=2, row=1, padx=5, pady=5)
29+
self.ms = ttk.Button(self.root, text='Merge Sort', style='info.TButton', padding=5, width=15,
30+
command=self.merge)
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)
35+
self.qs = ttk.Button(self.root, text='Quick Sort', style='info.TButton', padding=5, width=15,
36+
command=self.quick)
37+
self.qs.grid(column=5, row=1, padx=5, pady=5)
38+
39+
self.start = ttk.Button(self.root, text='Start', padding=5, width=15,
40+
command=self.start)
41+
self.start.grid(column=5, row=2, padx=5, pady=5)
42+
43+
44+
45+
self.shuf = ttk.Button(self.root, text='Shuffle', style='info.TButton', padding=5, width=15,
46+
command=self.shuffle)
47+
self.shuf.grid(column=0, row=2, padx=5, pady=5)
48+
49+
# Canvas
50+
self.canvas=Canvas(self.root, width=800-5, height=400,
51+
bg='white')
52+
self.canvas.grid(row=4, padx=5, pady=10, columnspan=6)
53+
54+
# some constants
55+
self.N=30
56+
self.colours=['blue' for i in range(self.N)]
57+
N=self.N
58+
self.data=np.linspace(5,400,N,dtype=np.uint16)
59+
np.random.shuffle(self.data)
60+
self.display(self.N,self.data,self.colours)
61+
62+
63+
def display(self,N,a,rong):
64+
self.canvas.delete('all')
65+
width=(2*(780/N))//3
66+
gap=(780/N)//3
67+
for i in range(N):
68+
self.canvas.create_rectangle(0+i*width+i*gap,0,0+(i+1)*width+i*gap,a[i],fill=rong[i])
69+
70+
self.root.update_idletasks()
71+
72+
73+
''' bubble sort'''
74+
def bubble(self):
75+
if self.st['bubble'] is False:
76+
self.st['bubble'] = True
77+
self.bs.config(style='success.TButton')
78+
79+
for i in self.st:
80+
if i != 'bubble':
81+
self.st[i]=False
82+
83+
self.qs.config(style='info.TButton')
84+
self.buckets.config(style='info.TButton')
85+
self.ms.config(style='info.TButton')
86+
self.ss.config(style='info.TButton')
87+
self.Is.config(style='info.TButton')
88+
# print(self.st)
89+
else:
90+
self.st['bubble'] = False
91+
self.bs.config(style='info.TButton')
92+
# print(self.st)
93+
94+
''' merge sort'''
95+
def merge(self):
96+
if self.st['merge'] is False:
97+
self.st['merge'] = True
98+
self.ms.config(style='success.TButton')
99+
100+
for i in self.st:
101+
if i != 'merge':
102+
self.st[i]=False
103+
104+
self.qs.config(style='info.TButton')
105+
self.buckets.config(style='info.TButton')
106+
self.bs.config(style='info.TButton')
107+
self.ss.config(style='info.TButton')
108+
self.Is.config(style='info.TButton')
109+
# print(self.st)
110+
else:
111+
self.st['merge'] = False
112+
self.ms.config(style='info.TButton')
113+
# print(self.st)
114+
115+
''' quick sort'''
116+
def quick(self):
117+
if self.st['quick'] is False:
118+
self.st['quick'] = True
119+
self.qs.config(style='success.TButton')
120+
121+
for i in self.st:
122+
if i != 'quick':
123+
self.st[i]=False
124+
125+
self.ms.config(style='info.TButton')
126+
self.buckets.config(style='info.TButton')
127+
self.bs.config(style='info.TButton')
128+
self.ss.config(style='info.TButton')
129+
self.Is.config(style='info.TButton')
130+
# print(self.st)
131+
else:
132+
self.st['quick'] = False
133+
self.qs.config(style='info.TButton')
134+
# print(self.st)
135+
136+
''' selection sort'''
137+
def selection(self):
138+
if self.st['selection'] is False:
139+
self.st['selection'] = True
140+
self.ss.config(style='success.TButton')
141+
142+
for i in self.st:
143+
if i != 'selection':
144+
self.st[i]=False
145+
146+
self.qs.config(style='info.TButton')
147+
self.buckets.config(style='info.TButton')
148+
self.bs.config(style='info.TButton')
149+
self.ms.config(style='info.TButton')
150+
self.Is.config(style='info.TButton')
151+
# print(self.st)
152+
else:
153+
self.st['selection'] = False
154+
self.ss.config(style='info.TButton')
155+
# print(self.st)
156+
157+
''' insertion sort'''
158+
def insertion(self):
159+
if self.st['insertion'] is False:
160+
self.st['insertion'] = True
161+
self.Is.config(style='success.TButton')
162+
163+
164+
for i in self.st:
165+
if i != 'insertion':
166+
self.st[i]=False
167+
self.qs.config(style='info.TButton')
168+
self.buckets.config(style='info.TButton')
169+
self.bs.config(style='info.TButton')
170+
self.ss.config(style='info.TButton')
171+
self.ms.config(style='info.TButton')
172+
# print(self.st)
173+
else:
174+
self.st['insertion'] = False
175+
self.Is.config(style='info.TButton')
176+
# print(self.st)
177+
178+
''' bucket sort'''
179+
def bucket(self):
180+
if self.st['bucket'] is False:
181+
self.st['bucket'] = True
182+
self.buckets.config(style='success.TButton')
183+
184+
for i in self.st:
185+
if i != 'bucket':
186+
self.st[i]=False
187+
188+
self.qs.config(style='info.TButton')
189+
self.Is.config(style='info.TButton')
190+
self.bs.config(style='info.TButton')
191+
self.ss.config(style='info.TButton')
192+
self.ms.config(style='info.TButton')
193+
# print(self.st)
194+
else:
195+
self.st['bucket'] = False
196+
self.buckets.config(style='info.TButton')
197+
# print(self.st)
198+
199+
def shuffle(self):
200+
self.canvas.delete('all')
201+
np.random.shuffle(self.data)
202+
self.display(self.N,self.data,self.colours)
203+
# print(self.data)
204+
205+
def start(self):
206+
if self.st['bubble'] is True:
207+
for i in range(self.N-1):
208+
for j in range(self.N-1-i):
209+
if self.data[j]>self.data[j+1]:
210+
self.data[j],self.data[j+1]=self.data[j+1],self.data[j]
211+
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)])
212+
time.sleep(0.02)
213+
self.display(self.N,self.data,['green' for _ in range(self.N)])
214+
215+
elif self.st['insertion'] is True:
216+
for j in range(1,len(self.data)):
217+
key=self.data[j]
218+
i=j-1
219+
while i>=0 and self.data[i]>key:
220+
self.data[i+1]=self.data[i]
221+
i-=1
222+
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)])
223+
time.sleep(0.02)
224+
self.data[i+1]=key
225+
self.display(self.N,self.data,['green' for _ in range(self.N)])
226+
227+
elif self.st['selection'] is True:
228+
for i in range(len(self.data)-1):
229+
min_index=i
230+
# loop to find the minimum element and its index
231+
for j in range(i+1,len(self.data)):
232+
if self.data[min_index]>self.data[j]:
233+
self.display(self.N,self.data,['purple' if a==j else 'green' if a<=i else 'blue' for a in range(self.N)])
234+
time.sleep(0.2)
235+
min_index=j
236+
if min_index!=i:
237+
self.data[i], self.data[min_index]=self.data[min_index],self.data[i]
238+
self.display(self.N,self.data,['green' for _ in range(self.N)])
239+
240+
241+
elif self.st['merge'] is True:
242+
pass
243+
244+
elif self.st['quick'] is True:
245+
pass
246+
247+
elif self.st['bucket'] is True:
248+
pass
249+
250+
else:
251+
'''show messege box'''
252+
pass
253+
254+
# print(self.data)
255+
256+
257+
win = Style(theme='cyborg').master
258+
obj = window(win, 'Sorting Algorithm Visualizer')
259+
260+
win.mainloop()

0 commit comments

Comments
(0)

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