-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
67 lines (58 loc) · 2.33 KB
/
algorithms.py
File metadata and controls
67 lines (58 loc) · 2.33 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
# algorithms.py
def bubble_sort(data, draw_callback, speed_ms, root):
n = len(data)
def sort_step(i, j):
if i < n:
if j < n - i - 1:
if data[j] > data[j + 1]:
data[j], data[j + 1] = data[j + 1], data[j]
colors = ['#e74c3c' if x == j or x == j + 1 else '#3498db' for x in range(n)]
draw_callback(data, colors)
root.after(speed_ms, sort_step, i, j + 1)
else:
root.after(speed_ms, sort_step, i + 1, 0)
else:
draw_callback(data, ['#2ecc71'] * n) # Sorted color
sort_step(0, 0)
def selection_sort(data, draw_callback, speed_ms, root):
n = len(data)
def sort_step(i, j, min_index):
if i < n - 1:
if j < n:
if data[j] < data[min_index]:
min_index = j
colors = ['#e74c3c' if x == min_index or x == j else '#3498db' for x in range(n)]
draw_callback(data, colors)
root.after(speed_ms, sort_step, i, j + 1, min_index)
else:
data[i], data[min_index] = data[min_index], data[i]
root.after(speed_ms, sort_step, i + 1, i + 2, i + 1)
else:
draw_callback(data, ['#2ecc71'] * n) # Sorted color
sort_step(0, 1, 0)
def insertion_sort(data, draw_callback, speed_ms, root):
n = len(data)
def sort_step(i, j, key):
if i < n:
if j >= 0 and data[j] > key:
data[j + 1] = data[j]
colors = ['#e74c3c' if x == j or x == j + 1 else '#3498db' for x in range(n)]
draw_callback(data, colors)
root.after(speed_ms, sort_step, i, j - 1, key)
else:
data[j + 1] = key
i += 1
if i < n:
key = data[i]
j = i - 1
colors = ['#e74c3c' if x == i else '#3498db' for x in range(n)]
draw_callback(data, colors)
root.after(speed_ms, sort_step, i, j, key)
else:
draw_callback(data, ['#2ecc71'] * n)
else:
draw_callback(data, ['#2ecc71'] * n)
if n > 0:
sort_step(1, 0, data[1])
else:
draw_callback(data, ['#2ecc71'] * n)