-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_sim.py
216 lines (166 loc) · 7.6 KB
/
sort_sim.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from tkinter import *
from PIL import ImageTk, Image
from tkinter import ttk
import random
from sortingAlgos import bubble_sort, insertion_sort, selection_sort
root = Tk()
root.title('IPSA Simulator')
root.iconbitmap('3.ico')
root.configure(background = 'black')
root.minsize(307,212)
root.maxsize(320,212)
#Variables
Algorithm_selected_var = StringVar()
data = []
#Title
title = Label(root, text="IPSA SIMULATOR", fg='white', bg='black', font=('Times', '18', "underline"))
title.grid(row=0, column=0, pady=15, columnspan=3)
#_____________________________________________TOP MAIN UI______________________________________________
#MAIN UI Function
def open_mainUI():
#DEFINE Toplevel()
top = Toplevel()
top.title("IPSA Simulator")
top.iconbitmap('3.ico')
#Fixed window size
top.minsize(593,492)
# top.maxsize(595,493)
top.configure(background='#444')
#Disabling widgets of root UI
select_btn.configure(state=DISABLED)
algo_menu.configure(state=DISABLED)
speedScale.configure(state=DISABLED)
#top frame
top_UI_frame = Frame(top, width=500, height=150, bg='#333')
top_UI_frame.grid(row=0, column=0)
#FUNCTIONS
def drawGraph(data, barColor):
#delete previous data if present
canvas.delete('all')
canvasHeight = 300
canvasWidth = 500
#width of bar graphs to be generated
barGraphWidth = canvasWidth / (len(data) + 1)
#bar graph should not start at border
offset = 60
#spacing b/w bars
spacing = 10
#Normalizing size of bar graph
normalizedData = [ i/max(data) for i in data]
#iterating through the data
for i, heightBar in enumerate(normalizedData):
#coords for creating BarGraph
#topLeft coords
x1 = i*barGraphWidth + offset + spacing
y1 = canvasHeight - heightBar*250
#bottomRight coords
x2 = (i+1)*barGraphWidth + offset
y2 = canvasHeight
canvas.create_rectangle(x1, y1, x2, y2, fill=barColor[i])
#number written over the Bar Graph
canvas.create_text(x1, y1, anchor=SW, text=str(data[i]))
#updating top window after drawing data after every single change
top.update_idletasks()
def generate():
global data
if sizeInput.get()=='' and minValue.get()=='' and maxValue.get()=='':
#Importing values from Input Data Entry Box
userData = list(inputData.get().split())
ipData = [ int(i) for i in userData]
data = ipData
else:
inputData.delete(0, END)
#Importing values from Entry Boxes
size = int(sizeInput.get())
minVal = int(minValue.get())
maxVal = int(maxValue.get())
#removing errors
if minVal > maxVal:
minVal, maxVal = maxVal, minVal
if minVal < 0:
minVal = 0
if maxVal > 100:
maxVal = 100
if size > 15:
size = 15 #maxm 15 values only be generated randomly
if size < 3:
size = 5
createdData = []
#creating random data of given size
for i in range(0, size):
createdData.append(random.randrange(minVal, maxVal+1))
data = createdData
#initially whole created array is red(#800000)
drawGraph(data, ['#800000' for x in range(len(data))])
def startAlgorithm():
global data
#when empty data array
if not data:
return
else:
if Algorithm_selected_var.get() == "Bubble Sort":
bubble_sort(data, drawGraph, speedScale.get())
elif Algorithm_selected_var.get() == "Insertion Sort":
insertion_sort(data, drawGraph, speedScale.get())
elif Algorithm_selected_var.get() == "Selection Sort":
selection_sort(data, drawGraph, speedScale.get())
drawGraph(data, ['#4c602a' for x in range(len(data))])
def close_topUI():
top.destroy()
#Enabling Select button of root UI
select_btn.configure(state=NORMAL)
algo_menu.configure(state=NORMAL)
speedScale.configure(state=NORMAL)
#Row[0] on top_UI_frame --> Row[0] on Top Level
#Size Entry
Label(top_UI_frame, text='Size: ', bg='#333', fg='#fff', font=('Helvetica', '10', 'bold')).grid(row=0, column=0, padx=10, pady=10, sticky=W)
sizeInput = Entry(top_UI_frame, width=10, font=('Helvetica', '10', 'bold'))
sizeInput.grid(row=0, column=1, padx=10, pady=10, sticky=W)
#Min. Value Entry
Label(top_UI_frame, text='Min. Value: ', bg='#333', fg='#fff', font=('Helvetica', '10', 'bold')).grid(row=0, column=2, padx=10, pady=10, sticky=W)
minValue = Entry(top_UI_frame, width=10, font=('Helvetica', '10', 'bold'))
minValue.grid(row=0, column=3, padx=10, pady=10, sticky=W)
#Max. Value Entry
Label(top_UI_frame, text='Max. Value: ', bg='#333', fg='#fff', font=('Helvetica', '10', 'bold')).grid(row=0, column=4, padx=10, pady=10, sticky=W)
maxValue = Entry(top_UI_frame, width=10, font=('Helvetica', '10', 'bold'))
maxValue.grid(row=0, column=5, padx=10, pady=10, sticky=W)
#Row[1] on top_UI_frame
Label(top_UI_frame, text='Input Data: ', bg='#333', fg='#fff', font=('Helvetica', '10', 'bold')).grid(row=1, column=0, padx=10, pady=10, sticky=W)
inputData = Entry(top_UI_frame, width=10, font=('Helvetica', '10', 'bold'))
inputData.grid(row=1, column=1, padx=10, pady=10, columnspan=2, sticky=W+E)
#Generate Data Button
Button(top_UI_frame, text="Generate Data", font=('Helvetica', '8', 'bold'), command=generate, bg='#4c602a', fg='#fff').grid(row=1, column=4, pady=10, sticky=W+E, columnspan=2)
#Row[2] on top_UI_frame
Button(top_UI_frame, text="START Simulation", font=('Helvetica', '8', 'bold'), command=startAlgorithm, bg='#800000', fg='#fff').grid(row=2, column=0, pady=10, sticky=W+E, columnspan=6)
#Row[1]
#CANVAS on Toplevel
canvas = Canvas(top, width=570, height=300, bg='white')
canvas.grid(row=1, column=0, padx=10, pady=5)
#Row[3] Close Button
close_top_btn = Button(top, text="CLOSE", font=('Helvetica', '8', 'bold'), command=close_topUI)
close_top_btn.grid(row=2, column=0, pady=10, sticky=W+E)
#________________________________________________________________________________________________________________________________________________________________
#INITIAL UI
Initial_UI_frame = Frame(root, width=250, height=100, bg='#333')
Initial_UI_frame.grid(row=1, column=0)
#Initial UI area
#Dropdown for selecting Algorithm
options_algo = [
"Bubble Sort",
"Insertion Sort",
"Selection Sort"
]
Algorithm_selected_var.set("Bubble Sort")
#Algorithm Label
Label(Initial_UI_frame, text='Algorithm: ', bg='#333', fg='#fff', font=('Helvetica', '10', "bold")).grid(row=1, column=0, padx=10, pady=10, sticky=W)
#OptionMenu
algo_menu = OptionMenu(Initial_UI_frame, Algorithm_selected_var, *options_algo)
algo_menu.grid(row=1, column=1, padx=20, pady=10, sticky=E)
#Simulation Speed Scale
Label(Initial_UI_frame, text='Simulation Speed [s]:', bg='#333', fg='#fff', font=('Helvetica', '10', "bold")).grid(row=2, column=0, padx=10, pady=10, sticky=W)
speedScale = Scale(Initial_UI_frame, from_=0.1, to=3.0, length=100, digits=2, resolution=0.2, orient=HORIZONTAL)
speedScale.grid(row=2, column=1, padx=5, pady=5)
#Select Button
select_btn = Button(Initial_UI_frame, text="Select", command=open_mainUI)
select_btn.grid(row=3, column=0, pady=10, columnspan=3, sticky=W+E)
root.mainloop()