-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting visualizer.py
132 lines (97 loc) · 4.89 KB
/
sorting visualizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from tkinter import *
from tkinter import ttk
import random
from sort import bubble_sort,selection_sort,insertion_sort,heapsort,mergesort,quick_sort
root = Tk()
root.title('Sorting Visualizer')
root.geometry('900x600+200+80')
root.config(bg='#082A46')
selected_algorithm = StringVar()
data = []
#code to generate the array
def Generate():
global data
print('Selected Algorithm: ' + selected_algorithm.get())
min_value = int(minvalue.get())
max_value = int(maxvalue.get())
size_value = int(sizevalue.get())
data = []
for i in range(size_value):
data.append(random.randrange(min_value, max_value + 1))
drawData(data, ['#A90042' for x in range(len(data))])
#drawing array elements as rectangle bars
def drawData(data, colorArray):
canvas.delete("all")
canvas_height = 450
canvas_width = 870
x_width = canvas_width / (len(data) + 1)
offset = 10
spacing_bet_rect = 10
normalized_data = [i / max(data) for i in data]
for i, height in enumerate(normalized_data):
x0 = i * x_width + offset + spacing_bet_rect
y0 = canvas_height - height * 400
x1 = (i + 1) * x_width
y1 = canvas_height
canvas.create_rectangle(x0, y0, x1, y1, fill=colorArray[i])
canvas.create_text(x0 + 2, y0, anchor=SW, text=str(data[i]), font=("new roman", 15, "italic bold"), fill="orange")
root.update_idletasks()
#code to start sorting
def StartAlgorithm():
global data
if not data:
return
if algo_menu.get() == 'Quick Sort':
quick_sort(data, 0, len(data)-1, drawData, speedscale.get())
elif algo_menu.get() == "Bubble Sort":
bubble_sort(data, drawData, speedscale.get())
elif algo_menu.get() == "Merge Sort":
mergesort(data, drawData, speedscale.get())
elif algo_menu.get() == "Heap Sort":
heapsort(data, drawData, speedscale.get())
elif algo_menu.get() == "Insertion Sort":
insertion_sort(data, drawData, speedscale.get())
elif algo_menu.get() == "Selection Sort":
selection_sort(data, drawData, speedscale.get())
drawData(data, ['gray' for x in range(len(data))])
#GUI PART
mainlabel = Label(root, text="Select Algorithm :", font=("new roman", 13, "bold"), bg="black",
height=1, width=15, fg="white")
mainlabel.place(x=5, y=20)
algo_menu = ttk.Combobox(root, width=15, font=("new roman", 17, "bold"), textvariable=selected_algorithm,
values=['Bubble Sort', 'Merge Sort', 'Quick Sort', 'Heap Sort', 'Insertion Sort', 'Selection Sort'])
algo_menu.place(x=170, y=20)
algo_menu.current(0)
random_generate = Button(root, text="Generate", bg="yellow", fg="black", font=("arial", 12, "italic bold"), relief=GROOVE,
activebackground="orange", activeforeground="white", bd=5, width=10, command=Generate)
random_generate.place(x=750, y=80)
sizevaluelabel = Label(root, text="Array size :", font=("new roman", 12, "bold"), bg="black",
height=2, width=10, fg="white", bd=5, relief=SUNKEN)
sizevaluelabel.place(x=4,y=80)
sizevalue = Scale(root, from_ = 0 , to = 30, resolution = 1,orient = HORIZONTAL, font = ("arial",14,"italic bold"),
relief = GROOVE, bd = 2, width = 10)
sizevalue.place(x=120,y=80)
minvaluelabel = Label(root, text="Min Value :", font=("new roman",12,"bold"),bg="black",
height=2, width=10,fg="white", bd=5, relief= SUNKEN)
minvaluelabel.place(x=250,y=80)
minvalue = Scale(root, from_ = 0 , to = 10, resolution = 1,orient = HORIZONTAL, font = ("arial",14,"italic bold"),
relief = GROOVE, bd = 2, width = 10)
minvalue.place(x=370,y=80)
maxvaluelabel = Label(root, text="Max Value :", font=("new roman",12,"bold"),bg="black",
height=2, width=10,fg="white", bd=5, relief= SUNKEN)
maxvaluelabel.place(x=500,y=80)
maxvalue = Scale(root, from_ = 0 , to = 100, resolution = 1,orient = HORIZONTAL, font = ("arial",14,"italic bold"),
relief = GROOVE, bd = 2, width = 10)
maxvalue.place(x=620,y=80)
start = Button(root,text="Start", bg="green", fg="white",font = ("arial",12,"italic bold"),relief = GROOVE,
activebackground="#C45B09", activeforeground="black",bd=5,width=10,command=StartAlgorithm)
start.place(x=750,y=20)
speedlabel = Label(root, text = "Speed :", font = ("new roman",12,"bold"), bg = "black",
height=2,width=10, fg="white",relief=SUNKEN,bd=5)
speedlabel.place(x=400,y=20)
speedscale = Scale(root, from_ = 0.1 , to = 5.0, resolution = 0.2,length=200,digits=2,orient = HORIZONTAL, font = ("arial",14,"italic bold"),
relief = GROOVE, bd = 2, width = 10)
speedscale.place(x=520,y=20)
canvas = Canvas(root, width=870, height=450, bg="black")
canvas.place(x=10,y=140)
root.mainloop()