- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.py
More file actions
Latest commit
158 lines (131 loc) · 4.82 KB
/
Copy pathvisualizer.py
File metadata and controls
158 lines (131 loc) · 4.82 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
fromtkinterimport*
fromtkinterimportmessagebox
fromtkinterimportttk
importrandom
importtime
frombubblesortimportbubble_sort
fromselectionsortimportselection_sort
frominsertionsortimportinsertion_sort
frommergesortimportmerge_sort
fromquicksortimportquick_sort
root=Tk()
root.title("Sorting Algorithm Visulaiser")
root.maxsize(900,600)
# background color
root.config(bg="black")
# variable
selected_algo=StringVar()
data= []
defdrawData(data, color):
canvas.delete("all")
c_height=380
c_width=580
# don't start from border
offset=10
# spacing between the bars
spacing=2
# width of th bar graph
x_width= (c_width-spacing*len(data))/ (len(data)) +1
normalisedData= [i/max(data) foriindata ]
prev=offset
x=0
fori, heightinenumerate(normalisedData):
# top left
# print(str(i) + " " + str(x_width)+ " " + color[i])
x0=prev+spacing
y0=c_height-height*340
# bottom right
x1=x0+x_width
y1=c_height
# creating the bars
canvas.create_rectangle(x0, y0, x1, y1, fill=color[i])
prev=x1
root.update_idletasks()
defGenerate():
globaldata
try:
minVal=max(int(minEntry.get()),0)
except:
minVal=3
try:
maxVal=min(int(maxEntry.get()),1000)
except:
maxVal=1000
try:
size=min(200,int(sizeEntry.get()))
ifsize<0:
size=200
except:
size=200
data= []
# generating random list
color=[]
for_inrange (size):
data.append(random.randrange(minVal, maxVal+1))
color.append('sky blue')
drawData(data, color)
defStartAlgorithm():
globaldata
speed=int(speedScale.get())
ifspeed==2:
speed=0.5
elifspeed==3:
speed=0.1
elifspeed==4:
speed=0.05
elifspeed==5:
speed=0.01
elifspeed==6:
speed=0.005
elifspeed==7:
speed=0.001
ifselected_algo.get() =="Bubble Sort" :
bubble_sort(data, drawData, speed)
elifselected_algo.get() =="Selection Sort" :
selection_sort(data, drawData, speed)
elifselected_algo.get() =="Insertion Sort" :
insertion_sort(data, drawData, speed)
elifselected_algo.get() =="Merge Sort" :
merge_sort(data, 0, len(data)-1, drawData, speed)
drawData(data, ['light green'forxinrange(len(data))])
time.sleep(speed)
elifselected_algo.get() =="Quick Sort" :
quick_sort(data, drawData, speed)
defmabout():
messagebox._show(title="About Me", _icon=None, message=" Name: Diptayan Biswas\n Email: diptayan2k@gmail.com\n Codechef Handle: diptayan\n Codeforces Handle: kakarotto_sama\n Ratings:\n - 6* at Codechef\n - Expert at Codeforces")
# frame / base layout
UI_frame=Frame(root, width=600, height=200, bg='green')
UI_frame.grid(row=0,column=0, padx=0 ,pady=5)
canvas=Canvas(root, width=800, height=380, bg='white')
canvas.grid(row=1, column=0, padx=10, pady=5)
# user interface area
# row[0]
# size of data
Label(UI_frame , text="Size of Data : " , bg='Green').grid(row=0, column=0, padx=5, pady=5, sticky=W)
sizeEntry=Entry(UI_frame)
sizeEntry.grid(row=0, column=1, padx=5, pady=5, sticky=W)
# minimum value of data
Label(UI_frame , text="Minimum Value: " , bg='Green').grid(row=0, column=2, padx=5, pady=5, sticky=W)
minEntry=Entry(UI_frame)
minEntry.grid(row=0, column=3, padx=5, pady=5, sticky=W)
# maximum value of data
Label(UI_frame , text="Maximum Value: " , bg='Green').grid(row=0, column=4, padx=5, pady=5, sticky=W)
maxEntry=Entry(UI_frame)
maxEntry.grid(row=0, column=5, padx=5, pady=5, sticky=W)
# Generate button
Button(UI_frame, text="Generate", command=Generate, bg='yellow').grid(row=0, column=6, padx=5, pady=5)
# row[1]
Label(UI_frame , text="Select Algorithm" , bg='Green').grid(row=1, column=0, padx=5, pady=5, sticky=W)
# Drop down menu for algorithm selection
algMenu=ttk.Combobox(UI_frame, textvariable=selected_algo, values=['Bubble Sort', 'Selection Sort', 'Insertion Sort', 'Merge Sort', 'Quick Sort'])
algMenu.grid(row=1, column=1, padx=5, pady=5)
# In case no algorithm is selected, the default value is first option
algMenu.current(0)
# speed scale
speedScale=Scale(UI_frame,from_=1, to=7, length=200, digits=2, resolution=1, orient=HORIZONTAL, label="Select Speed :", bg="sky blue")
speedScale.grid(row=1, column=3, padx=5, pady=5)
# start button
Button(UI_frame, text="Start", command=StartAlgorithm, bg='red').grid(row=1, column=4, padx=5, pady=5)
# About button
Button(UI_frame, text="About Me", command=mabout, bg='red').grid(row=1, column=5, padx=5, pady=5)
root.mainloop()