-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
65 lines (56 loc) · 2.1 KB
/
controller.py
File metadata and controls
65 lines (56 loc) · 2.1 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
# controller.py
import random
import time
from tkinter import Canvas
from algorithms import bubble_sort, selection_sort, insertion_sort
array = []
canvas_width = 800
canvas_height = 400
speed_map = {
"Fast": 0.001,
"Medium": 0.1,
"Slow": 0.3
}
def draw_data(canvas: Canvas, data, color_array):
canvas.delete("all")
bar_width = max(15, canvas_width // len(data))
spacing = 4
offset = 30
for i, val in enumerate(data):
x0 = i * (bar_width + spacing) + spacing
y0 = canvas_height - val
x1 = x0 + bar_width
y1 = canvas_height
canvas.create_rectangle(x0, y0, x1, y1, fill=color_array[i], outline="")
canvas.create_text((x0 + x1) / 2, y0 - 12, text=str(val), anchor='s', font=("Helvetica", 10, "bold"))
canvas.update_idletasks()
def generate_data(canvas, show_sort_button_callback):
global array
array = [random.randint(10, 300) for _ in range(20)]
draw_data(canvas, array, ["#3498db"] * len(array))
show_sort_button_callback()
def on_sort_click(canvas, algorithm, speed, root):
if not array:
return
speed_map_ms = {
"Fast": 10, # 10 ms delay
"Medium": 100, # 100 ms delay
"Slow": 300 # 300 ms delay
}
speed_ms = speed_map_ms.get(speed, 10)
if algorithm == "Bubble Sort":
bubble_sort(array, lambda d, c: draw_data(canvas, d, c), speed_ms, root)
elif algorithm == "Selection Sort":
selection_sort(array, lambda d, c: draw_data(canvas, d, c), speed_ms, root)
elif algorithm == "Insertion Sort":
insertion_sort(array, lambda d, c: draw_data(canvas, d, c), speed_ms, root)
# def on_sort_click(canvas, algorithm, speed):
# if not array:
# return
# delay = lambda: time.sleep(speed_map[speed])
# if algorithm == "Bubble Sort":
# bubble_sort(array, lambda d, c: draw_data(canvas, d, c), delay)
# elif algorithm == "Selection Sort":
# selection_sort(array, lambda d, c: draw_data(canvas, d, c), delay)
# elif algorithm == "Insertion Sort":
# insertion_sort(array, lambda d, c: draw_data(canvas, d, c), delay)