Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
221 lines (169 loc) · 6.5 KB
/
Copy pathmain.py
File metadata and controls
221 lines (169 loc) · 6.5 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
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
218
219
220
221
import random
import time
from tkinter import *
from tkinter import ttk
# import sorting algorithms
from algorithms.bubbleSort import bubble_sort
from algorithms.insertionSort import insertion_sort
from algorithms.mergeSort import merge_sort
from algorithms.quickSort import quick_sort
from algorithms.selectionSort import selection_sort
from algorithms.shellSort import shell_sort
# import colors to use throughout the project
from colors import *
window = Tk()
window.title("Sorting Algorithm Visualizer By: Vicky Sekhon")
CANVAS_WIDTH = 1900
CANVAS_HEIGHT = 700
OFFSET = 4
SPACING = 2
window.maxsize(1920, 1130)
window.config(background=BLACK) # set background color
COLORS = [RED, BLUE, LIGHT_GREEN, YELLOW, DARK_BLUE, WHITE, PINK, PURPLE]
START_TIME = None
END_TIME = None
algorithm_name = StringVar()
# define algorithms
algorithm_list = [
"Bubble Sort",
"Insertion Sort",
"Merge Sort",
"Quick Sort",
"Selection Sort",
"Shell Sort",
]
speed_name = StringVar()
speed_list = ["Fast", "Medium", "Slow"] # define sorting speeds
data = [] # array to store data
def update_elapsed_time():
elapsed_time = 0
if (START_TIME != None and END_TIME != None):
elapsed_time = END_TIME - START_TIME
elapsed_time_string = time.strftime("%S", time.gmtime(elapsed_time))
sorting_time.config(text=elapsed_time_string + " seconds")
else:
sorting_time.config(text="")
def draw_single_data_element(element, height, x_width, colorArray):
# starting x coordinate
x0 = element * x_width + OFFSET + SPACING
# starting y coordinate
y0 = CANVAS_HEIGHT - height * 890
# ending x coordinate
x1 = (element + 1) * x_width + OFFSET
# ending y coordinate
y1 = CANVAS_HEIGHT
# draw rectangle using starting and ending x,y coordinates
canvas.create_rectangle(x0, y0, x1, y1, fill=colorArray[element])
# display array
def draw_array(data, colorArray):
# clear canvas
canvas.delete("all")
# width to ensure data elements are included on the canvas
x_width = CANVAS_WIDTH / (len(data) + 1)
# create sizing relative to the largest data element
relativelySizedData = [i / max(data) for i in data]
for i, height in enumerate(relativelySizedData):
draw_single_data_element(i, height, x_width, colorArray)
window.update_idletasks()
# generate random values for the array
def generate():
global data
data = []
# get no. of data elements to generate
number_of_elements = data_elements.get()
# get max magnitude of data elements to generate
magnitude = max_magnitude.get()
# check if no. of data elements entered is valid
if number_of_elements.isalpha() or magnitude.isalpha():
print("Invalid input")
return
else:
number_of_elements = int(number_of_elements)
magnitude = int(magnitude)
# add data elements to array to sort
for i in range(0, number_of_elements):
data.append(random.randint(25, magnitude))
# draw the array onto the screen
draw_array(data, [COLORS[random.randint(0, 7)] for x in range(len(data))])
# sorting speed
def sort_speed():
if speed_menu.get() == "Slow":
return 0.3
elif speed_menu.get() == "Medium":
return 0.1
else:
return 0.001
# sort using desired algorithm
def sort():
global data, START_TIME, END_TIME
timeTick = sort_speed()
START_TIME = time.time()
if algorithm_menu.get() == "Bubble Sort":
bubble_sort(data, draw_array, timeTick)
elif algorithm_menu.get() == "Merge Sort":
merge_sort(data, 0, len(data) - 1, draw_array, timeTick)
elif algorithm_menu.get() == "Insertion Sort":
insertion_sort(data, draw_array, timeTick)
elif algorithm_menu.get() == "Shell Sort":
shell_sort(data, draw_array, timeTick)
elif algorithm_menu.get() == "Selection Sort":
selection_sort(data, draw_array, timeTick)
elif algorithm_menu.get() == "Quick Sort":
quick_sort(data, 0, len(data) - 1, draw_array, timeTick)
END_TIME = time.time()
update_elapsed_time()
# user interactable elements
UI_frame = Frame(window, width=900, height=300, background=BLACK)
UI_frame.grid(row=0, column=0, padx=10, pady=5)
# styling for the combobox to match the black theme
style = ttk.Style()
style.theme_use("clam")
style.configure("TCombobox", fieldbackground=BLACK, background=WHITE, foreground=WHITE)
# title
l0 = Label(
UI_frame,
text="Sorting Algorithm By Vicky Sekhon",
font=100,
background=BLACK,
foreground=WHITE,
)
l0.grid(row=0, column=0, columnspan=3, padx=10, pady=5, sticky=W)
# algorithm list
l1 = Label(UI_frame, text="Algorithm: ", background=BLACK, foreground=WHITE)
l1.grid(row=1, column=0, padx=10, pady=5, sticky=W)
algorithm_menu = ttk.Combobox(
UI_frame, textvariable=algorithm_name, values=algorithm_list
)
algorithm_menu.grid(row=1, column=1, padx=5, pady=5)
algorithm_menu.current(0)
# speed list
l2 = Label(UI_frame, text="Sorting Speed: ", background=BLACK, foreground=WHITE)
l2.grid(row=2, column=0, padx=10, pady=5, sticky=W)
speed_menu = ttk.Combobox(UI_frame, textvariable=speed_name, values=speed_list)
speed_menu.grid(row=2, column=1, padx=5, pady=5)
speed_menu.current(0)
# no. of data elements input box
l3 = Label(UI_frame, text="Data Elements: ", background=BLACK, foreground=WHITE)
l3.grid(row=3, column=0, padx=10, pady=5, sticky=W)
data_elements = ttk.Entry(UI_frame)
data_elements.grid(row=3, column=1, padx=5, pady=5)
# max magnitude of data elements input box
l4 = Label(UI_frame, text="Max Magnitude: ", background=BLACK, foreground=WHITE)
l4.grid(row=4, column=0, padx=10, pady=5, sticky=W)
max_magnitude = ttk.Entry(UI_frame)
max_magnitude.grid(row=4, column=1, padx=5, pady=5)
# sorting button
b1 = Button(UI_frame, text="Sort", command=sort, background=WHITE)
b1.grid(row=5, column=1, padx=5, pady=5)
# array generator button
b3 = Button(UI_frame, text="Generate Array", command=generate, background=WHITE)
b3.grid(row=5, column=0, padx=5, pady=5)
# sorting time text
l5 = Label(UI_frame, text="Elapsed Time:", background=BLACK, foreground=WHITE, font=100)
l5.grid(row=5, column=3, padx=10, pady=5, sticky=W)
sorting_time = ttk.Label(UI_frame, text="", background=BLACK, foreground=WHITE, font=100)
sorting_time.grid(row=5, column=4, padx=10, pady=5, sticky=W)
# canvas
canvas = Canvas(window, width=2000, height=1000, background=BLACK, highlightthickness=0)
canvas.grid(row=6, column=0, columnspan=3, padx=10, pady=5)
window.mainloop()