-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
207 lines (157 loc) · 6.55 KB
/
main.py
File metadata and controls
207 lines (157 loc) · 6.55 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
import random
from tkinter import *
from tkinter import font
from Sorting.bubblesort import BubbleSort
from Sorting.cocktailsort import CocktailSort
from Sorting.combsort import CombSort
from Sorting.heapsort import HeapSort
from Sorting.insertionsort import InsertionSort
from Sorting.mergesort import MergeSort
from Sorting.monkeysort import MonkeySort
from Sorting.quicksort import QuickSort
from Sorting.selectionsort import SelectionSort
from Sorting.shellsort import ShellSort
algoList = ['Monkey Sort', 'Bubble Sort', 'Selection Sort', 'Insertion Sort', 'Cocktail Sort', 'Comb Sort', 'Shell Sort', 'Heap Sort', 'Quick Sort', 'Merge Sort']
speedList = ['Noma']
bars, data = [], [i for i in range(1, 11)]
red = '#ff0000'
green = '#00b32d'
black = '#000000'
white = '#ffffff'
grey = '#a6a2a2'
class Application(Tk):
def __init__(self, parent) -> None:
Tk.__init__(self)
self.parent = parent
self.geometry('1920x1080')
self.title('Sorting Algorithms')
self.upper = Upper(self)
self.lower = Lower(self)
class Lower(Frame):
def __init__(self, parent) -> None:
Frame.__init__(self, master=parent, bg=black, width=1920, height=950)
# self.graph = graph
self.parent = parent
self.grid(row=1, column=0)
self.grid_propagate(False)
self.populate()
def populate(self):
# sliders
self.size_slider = Scale(self, from_=10, to=250, orient='horizontal', label='Size of Array', length=250, command=self.parent.upper.graph.generate_data)
self.size_slider.grid(row=0, column=0, padx=70, pady=40)
self.time_slider = Scale(self, from_=1, to=10, orient=HORIZONTAL, label='Sorting Speed', length=250, resolution=1, tickinterval=1, showvalue=0)
self.time_slider.grid(row=0, column=5, padx=70)
# buttons
self.shuffle_button = Button(self, text='Shuffle Array', height=5, width=30, command=self.parent.upper.graph.shuffle)
self.shuffle_button.grid(row=0, column=3, padx=70)
self.sort_button = Button(self, text='Sort', height=5, width=30, command=self.parent.upper.graph.begin_sort)
self.sort_button.grid(row=0, column=4, padx=70)
# dropdown
font.Font(family='Helvetica', name='hel')
self.variable = StringVar(self)
self.variable.set('Select an Algorithm')
self.dropdown = OptionMenu(self, self.variable, *algoList)
self.dropdown.configure(height=1, width=20, font='hel')
self.dropdown['menu'].config(font='hel')
self.dropdown.grid(row=0, column=6, padx=70)
def get_variable(self):
return self.variable.get()
def get_time_slider(self):
return self.time_slider.get()
class Upper(Frame):
def __init__(self, parent) -> None:
Frame.__init__(self, master=parent, bg=red, width=1920, height=950)
self.parent = parent
self.grid(row=0, column=0)
self.grid_propagate(False)
self.graph = Graph(self)
def add_graph(self, width, height, bg):
self.graph = Canvas(self, width=width, height=height, bg=bg)
self.graph.place(relx=0.5, rely=0.5, anchor=CENTER)
class Graph(Canvas):
def __init__(self, parent) -> None:
Canvas.__init__(self, master=parent, bg=black, width=1870, height=900)
self.place(relx=0.5, rely=0.5, anchor=CENTER)
self.parent = parent
self.width = 1870
self.height = 900
self.draw_data(data, [white for x in range(len(data))])
def generate_data(self, max_value):
global data
data = []
for i in range(1, int(max_value)+1):
data.append(i)
self.draw_data(data, [white for x in range(len(data))])
def shuffle(self):
global data
random.shuffle(data)
self.draw_data(data, [white for x in range(len(data))])
def draw_data(self, array, colors):
global bars
self.delete('all')
bar_width = self.width / len(array)
coefficient = (self.height - 2) / max(array)
for counter, num in enumerate(array):
x1 = counter * bar_width
x2 = x1 + bar_width
y1 = self.height - num * coefficient
y2 = self.height
bar = self.create_rectangle(x1, y1, x2, y2, fill=colors[counter])
bars.append(bar)
self.parent.parent.update_idletasks()
def begin_sort(self):
global data
function_dict = {
# select an algorithm error message in the window
'Monkey Sort': MonkeySort,
'Bubble Sort': BubbleSort,
'Selection Sort': SelectionSort,
'Insertion Sort': InsertionSort,
'Cocktail Sort': CocktailSort,
'Comb Sort': CombSort,
'Shell Sort': ShellSort,
'Heap Sort': HeapSort,
'Quick Sort': QuickSort,
'Merge Sort': MergeSort
}
if self.parent.parent.lower.get_variable() == 'Monkey Sort':
monke = []
while monke != sorted(data):
monke = function_dict[self.parent.parent.lower.get_variable()](monke, [white for x in range(len(monke))])
elif self.parent.parent.lower.get_variable() == 'Quick Sort':
QuickSort(data, 0, len(data)-1, self.draw_data)
else:
function_dict[self.parent.parent.lower.get_variable()](data, self.draw_data, (self.parent.parent.lower.time_slider.get()/10-0.1))
self.is_sorted()
def is_sorted(self):
global data
colors = [white for x in range(len(data))]
i = 0
if len(data) % 5 == 0:
while i < len(data):
colors[i], colors[i+1], colors[i+2], colors[i+3], colors[i+4] = green, green,green,green,green,
self.draw_data(data, colors)
i += 5
elif len(data) % 4 == 0:
while i < len(data):
colors[i], colors[i+1], colors[i+2], colors[i+3] = green,green,green,green,
self.draw_data(data, colors)
i += 4
elif len(data) % 3 == 0:
while i < len(data):
colors[i], colors[i+1], colors[i+2] = green,green,green,
self.draw_data(data, colors)
i += 3
elif len(data) % 2 == 0:
while i < len(data):
colors[i], colors[i+1] = green,green,
self.draw_data(data, colors)
i += 2
else:
while i < len(data):
colors[i] = green
self.draw_data(data, colors)
i += 1
if __name__ == "__main__":
app = Application(None)
app.mainloop()