-
Notifications
You must be signed in to change notification settings - Fork 1
/
sortingAlgos.py
217 lines (175 loc) · 5.76 KB
/
sortingAlgos.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
217
from tkinter import *
from tkinter import ttk
import random
import time
from sorts.bubbleSort import bubbleSort
from sorts.mergeSort import mergeSort
root = Tk()
root.title('PySORTO- Sorting Visualiser')
root.maxsize = (900, 600)
# purplish color
root.config(bg='#9000F0')
# Global Variables
data = []
selected_alg = StringVar()
# Base GUI/frame window
# User Console/window
UI_frame = Frame(root, width=600, height=200, bg='#FFB366')
UI_frame.grid(row=0, column=0, padx=10, pady=5)
# Visualization window
canvas = Canvas(root, width=600, height=380, bg='#FFFFFF')
canvas.grid(row=1, column=0, padx=10, pady=5)
# Runtime window
# Visualization window
runtime_canvas = Canvas(root, width=200, height=100, bg='#FFFFFF')
runtime_canvas.grid(row=2, column=0, padx=5, pady=5)
# test function
def drawData(data, colorArray):
start_time = time.time()
# delete old canvas
canvas.delete("all")
runtime_canvas.delete("all")
c_height = 400
c_width = 600
x_width = c_width / (len(data) + 1)
offset = 30
spacing = 20
# keep it less than c_height
barscalefactor = 300
# normalize the data array
normalizedData = []
for i in data:
temp = i/max(data)
print(i, temp)
normalizedData.append(temp)
# normalizedData = data
for i, height in enumerate(normalizedData):
# top left
x0 = i * x_width + offset + spacing
y0 = c_height - height * barscalefactor
# bottom right
x1 = (i+1) * x_width + offset
y1 = c_height
canvas.create_rectangle(x0, y0, x1, y1, fill=colorArray[i])
canvas.create_text((x0+2), y0, anchor=SW, text=str(data[i]))
# animation
# root.update()
# time.sleep(refreshVal)
# time.sleep(1)
root.update_idletasks()
# runtime canvas/window
print("Execution time: " + str(time.time() - start_time))
# runtime_canvas.create_rectangle(20, 20, 20, 20, fill="#FFFF66")
runtime_canvas.create_text(20, 20, anchor=SW,
text=("runtime( in seconds):" + str(time.time() -
start_time)))
def Generate():
global data
print("Running" + selected_alg.get())
# to generate user data
# generate and save random array in data[]
data = []
# Minimum value in dataset
try:
minVal = int(minEntry.get())
except:
print("User did not provide minimum value. Setting it to 0.")
minVal = 0
finally:
if minVal < 0:
minVal = 0
# Maximum value in dataset
try:
maxVal = int(maxEntry.get())
except:
maxVal = random.randrange(1, 1337)
print("User did not provide maximum value. Setting it to psudorandom value between 1 to 1337: " + str(maxVal))
finally:
if maxVal < 0:
maxVal = random.randrange(1, 1337)
# size of dataset
try:
sizeVal = int(sizeEntry.get())
except:
sizeVal = 42
finally:
if sizeVal < 1:
sizeVal = random.randrange(1, 1337)
'''
# refrsh rate
try:
refreshVal = float(refreshEntry.get())
except:
refreshVal = 0.25
finally:
if refreshVal < 0:
refreshVal = 0.25
'''
for _ in range(sizeVal):
data.append(random.randrange(minVal, maxVal + 1))
drawData(data, ['#FF0000' for x in range(len(data))])
def startAlgorithm():
global data
if not data:
print("Error: No dataset")
return
if algMenu.get() == "Merge Sort":
mergeSort(data, drawData, speedScale.get())
elif algMenu.get() == "Bubble Sort":
bubbleSort(data, drawData, speedScale.get())
drawData(data, ['green' for x in range(len(data))])
# UI
# row 1
Label(UI_frame, text="Algorithm: ", bg='#FFFF66').grid(
row=0, column=0, padx=5, pady=5, sticky=W)
algMenu = ttk.Combobox(UI_frame, textvariable=selected_alg,
values=['Bubble Sort', "Merge Sort"])
algMenu.grid(row=0, column=1, padx=5, pady=5)
algMenu.current(0)
# scale
speedScale = Scale(UI_frame, from_=0.0, to=2.0,
length=200, digits=2, resolution=0.2, orient=HORIZONTAL, label="Speed in secs")
speedScale.grid(row=0, column=2, padx=5, pady=5)
Button(UI_frame, text="Start", command=startAlgorithm,
bg='#9000F0').grid(row=0, column=3, padx=5, pady=5)
# row 2
'''
Label(UI_frame, text="Size: ", bg='#FFFF66').grid(
row=1, column=0, padx=5, pady=5, sticky=W)
sizeEntry = Entry(UI_frame)
'''
sizeEntry = Scale(UI_frame, from_=3, to=25,
resolution=0.2, orient=HORIZONTAL, label="Dataset size")
sizeEntry.grid(row=1, column=0, padx=5, pady=5
)
'''
Label(UI_frame, text="Minimum Value ", bg='#FFFF66').grid(
row=1, column=2, padx=5, pady=5, sticky=W)
minEntry = Entry(UI_frame)
'''
minEntry = Scale(UI_frame, from_=0, to=10,
resolution=0.2, orient=HORIZONTAL, label="Minimum value")
minEntry.grid(row=1, column=1, padx=5, pady=5
)
'''
Label(UI_frame, text="Maximum Value ", bg='#FFFF66').grid(
row=1, column=4, padx=5, pady=5, sticky=W)
maxEntry = Entry(UI_frame)
'''
maxEntry = Scale(UI_frame, from_=10, to=100,
resolution=0.2, orient=HORIZONTAL, label="Maximum value")
maxEntry.grid(row=1, column=2, padx=5, pady=5
)
'''
Label(UI_frame, text="Refresh rate in seconds ", bg='#FFFF66').grid(
row=1, column=6, padx=5, pady=5, sticky=W)
refreshEntry = Entry(UI_frame)
refreshEntry = Scale(UI_frame, from_=0, to=1.0,
resolution=0.2, orient=HORIZONTAL, label="refresh rate")
refreshEntry.grid(row=1, column=3, padx=5, pady=5
)
'''
Button(UI_frame, text="Generate", command=Generate,
bg='#9000F0').grid(row=1, column=4, padx=5, pady=5)
# main loop
root.mainloop()