from
tkinter
import
*
from
tkinter
import
ttk, messagebox
from
ttkbootstrap
import
*
import
numpy as np
import
time
N
=
50
speed
=
100
array
=
np.linspace(
10
,
390
, N, dtype
=
np.uint16)
def
display(n, array, color):
__canvas.delete(
'all'
)
width
=
1560
/
(
3
*
n
-
1
)
gap
=
width
/
2
for
i
in
range
(n):
__canvas.create_rectangle(
7
+
i
*
width
+
i
*
gap,
0
,
7
+
(i
+
1
)
*
width
+
i
*
gap, array[i],
fill
=
color[i])
root.update_idletasks()
def
shuffle():
np.random.shuffle(array)
display(N, array, colors)
def
start():
if
algos[
'selection'
]
=
=
True
:
for
i
in
range
(
len
(array)
-
1
):
min_index
=
i
for
j
in
range
(i
+
1
,
len
(array)):
display(N, array, [
'yellow'
if
a
=
=
min_index
or
a
=
=
i
else
'green'
if
a <
=
i
else
'cyan'
for
a
in
range
(N)])
time.sleep(
1
/
speed)
if
array[min_index] > array[j]:
display(N, array, [
'red'
if
a
=
=
min_index
or
a
=
=
j
else
'green'
if
a <
=
i
else
'cyan'
for
a
in
range
(N)])
time.sleep(
1
/
speed)
min_index
=
j
if
min_index !
=
i:
array[i], array[min_index]
=
array[min_index],
array[i]
display(N, array, [
'pink'
if
a
=
=
min_index
or
a
=
=
i
else
'green'
if
a <
=
i
else
'cyan'
for
a
in
range
(N)])
time.sleep(
1
/
speed)
display(N, array, [
'yellow'
for
_
in
range
(N)])
elif
algos[
'insertion'
]
=
=
True
:
for
j
in
range
(
1
,
len
(array)):
key
=
array[j]
i
=
j
-
1
display(N, array, [
'yellow'
if
a
=
=
i
or
a
=
=
i
+
1
else
'green'
if
a <
=
j
else
'cyan'
for
a
in
range
(N)])
time.sleep(
1
/
speed)
while
i >
=
0
and
array[i] > key:
array[i
+
1
]
=
array[i]
display(N, array, [
'pink'
if
a
=
=
i
else
'green'
if
a <
=
j
else
'cyan'
for
a
in
range
(N)])
time.sleep(
1
/
speed)
i
-
=
1
array[i
+
1
]
=
key
display(N, array, [
'yellow'
for
_
in
range
(N)])
else
:
messagebox.showerror(
"Algorithm Visualizer"
,
"You need to select a sorting algorithm"
)
def
slide():
global
speed
global
N
global
array
N
=
int
(_scale.get()
/
20
)
array
=
np.linspace(
5
,
400
, N, dtype
=
np.uint16)
speed
=
int
(_scale.get())
shuffle()
def
selection():
if
algos[
'selection'
]
is
False
:
algos[
'selection'
]
=
True
__selec.config(style
=
'success.TButton'
)
algos[
'insertion'
]
=
False
__in.config(style
=
'danger.TButton'
)
else
:
algos[
'selection'
]
=
False
__selec.config(style
=
'danger.TButton'
)
def
insertion():
if
algos[
'insertion'
]
is
False
:
algos[
'insertion'
]
=
True
__in.config(style
=
'success.TButton'
)
algos[
'selection'
]
=
False
__selec.config(style
=
'danger.TButton'
)
else
:
algos[
'insertion'
]
=
False
__in.config(style
=
'danger.TButton'
)
if
__name__
=
=
'__main__'
:
root
=
Style(theme
=
'superhero'
).master
algos
=
{
'insertion'
:
False
,
'selection'
:
False
}
root.title(
'Sorting visualizer'
)
root.resizable(
0
,
0
)
Label(root, text
=
'Selection and insertion sort visualization'
).grid(row
=
0
,column
=
0
, columnspan
=
8
)
__in
=
ttk.Button(root, text
=
'Insertion sort'
, width
=
16
,
padding
=
5
, command
=
insertion)
__in.grid(row
=
1
, column
=
0
, pady
=
20
)
__selec
=
ttk.Button(root, text
=
'Selection sort'
,
width
=
16
, padding
=
5
, command
=
selection)
__selec.grid(row
=
1
, column
=
1
, pady
=
20
)
__start
=
ttk.Button(root, text
=
'Start Sorting'
,
width
=
16
, padding
=
5
, command
=
start)
__start.grid(row
=
1
, column
=
2
, pady
=
20
)
__shuffle
=
ttk.Button(root, text
=
'Shuffle Array'
,
width
=
16
, padding
=
5
, command
=
shuffle)
__shuffle.grid(row
=
1
, column
=
3
, pady
=
20
)
_scale
=
ttk.Scale(root, from_
=
50
, to
=
1000
,
length
=
250
, value
=
1000
,
command
=
lambda
x: slide())
_scale.grid(row
=
1
, column
=
4
, columnspan
=
4
, padx
=
5
)
colors
=
[
'cyan'
for
_
in
range
(N)]
__canvas
=
Canvas(root, width
=
800
, height
=
400
)
__canvas.grid(row
=
2
, column
=
0
, columnspan
=
8
)
shuffle()
display(N, array, colors)
root.mainloop()