-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
115 lines (90 loc) · 3.56 KB
/
main.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
from tkinter import *
from tkinter import ttk
from algorithms.bubblesort import bubble_sort
from algorithms.mergesort import merge_sort
from algorithms.insertionsort import insertion_sort
from algorithms.quicksort import quick_sort
from algorithms.selectionsort import selection_sort
import random #for array creation
from colors import * # importing colors from colors.py
#main window
window = Tk()
window.title("Sorting Algorithm Visualizer")
window.maxsize(1000,700)
window.config(bg = BACKGROUND)
algorithm_name = StringVar()
algo_list = ['Selection Sort','Merge Sort','Insertion Sort','Quick Sort','Bubble Sort']
speed_name = StringVar()
speed_list = ['Fast','Medium','Slow']
data = [] #Empty list filled with random values everytime we generate new array
#Draws randomly generated list
def drawData(data,colorArray):
canvas.delete("all")
canvas_width = 800
canvas_height = 400
x_width = canvas_width / (len(data) + 1)
offset = 4
spacing = 2
normalizedData = [i / max(data) for i in data]
for i, height in enumerate(normalizedData):
x0 = i * x_width + offset + spacing
y0 = canvas_height - height * 390
x1 = (i + 1) * x_width + offset
y1 = canvas_height
canvas.create_rectangle(x0, y0, x1, y1, fill = colorArray[i])
window.update_idletasks()
#Generates array with random values every time "generate" is pressed
def generate():
global data
data = []
for i in range (0,100):
random_value = random.randint(1, 150)
data.append(random_value)
drawData(data, [BARS for x in range(len(data))])
#Sets sorting speed
def set_speed():
if speed_menu.get() == 'Slow' :
return 0.5
elif speed_menu.get() == 'Medium':
return 0.1
else:
return 0.0009
#Triggers selected algo and starts sorting
def sort():
global data
timeTick = set_speed()
if algo_menu.get() == 'Bubble Sort':
bubble_sort(data, drawData, timeTick)
elif algo_menu.get() == 'Merge Sort':
merge_sort(data, 0, len(data)-1, drawData, timeTick)
elif algo_menu.get() == 'Insertion Sort':
insertion_sort(data, drawData, timeTick)
elif algo_menu.get() == 'Quick Sort':
quick_sort(data, 0, len(data)-1,drawData, timeTick)
elif algo_menu.get() == 'Selection Sort':
selection_sort(data, drawData, timeTick)
#Interface
UI_frame = Frame(window, width = 900, height = 300, bg = BACKGROUND)
UI_frame.grid(row = 0, column = 0, padx = 10, pady = 5)
#dropdown to select sorting algorithm
l1 = Label(UI_frame, text = "Algorithm: ",bg = BACKGROUND,fg = WHITE)
l1.grid(row = 0, column = 0, padx = 10, pady = 5, sticky = W)
algo_menu = ttk.Combobox(UI_frame, textvariable = algorithm_name, values = algo_list)
algo_menu.grid(row = 0, column = 1, padx = 5, pady = 5)
algo_menu.current(0)
#dropdown for sorting speed
l2 = Label(UI_frame, text = "Sorting Speed: ",bg = BACKGROUND,fg = WHITE)
l2.grid(row = 1, column = 0, padx = 10, pady = 5, sticky = W)
speed_menu = ttk.Combobox(UI_frame, textvariable = speed_name, values = speed_list)
speed_menu.grid(row = 1, column = 1, padx = 5, pady = 5)
speed_menu.current(0)
#sort button
b1 = Button(UI_frame, text = "Sort", command = sort, bg = BUTTON,fg = WHITE)
b1.grid(row = 2, column = 1, padx = 5, pady = 5)
#Generate button
b2 = Button(UI_frame, text = "Generate Array", command = generate, bg = BUTTON,fg = WHITE)
b2.grid(row = 2, column = 0, padx = 5, pady = 5)
#Canvas to draw array
canvas = Canvas(window, width = 800, height = 400, bg = CANVAS)
canvas.grid(row = 1, column = 0, padx = 10, pady = 5)
window.mainloop()