Repository navigation
Expand file tree
/
Copy pathvisualizer.py
More file actions
executable file
·128 lines (101 loc) · 3.31 KB
/
Copy pathvisualizer.py
File metadata and controls
executable file
·128 lines (101 loc) · 3.31 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
import tkinter as tk
import random
import time
def draw_data(data, active_indices=None, index=-1, comparisons=0, swaps=0):
if active_indices is None:
active_indices = []
canvas.delete("all")
bar_width = 800 / len(data)
for i in range(len(data)):
height = data[i]
x0 = i * bar_width
y0 = 500 - height
x1 = (i + 1) * bar_width
y1 = 500
if i in active_indices:
bar_colour = "red"
else:
bar_colour = "white"
if i == index:
bar_colour = "green"
canvas.create_rectangle(x0, y0, x1, y1, fill=bar_colour, outline="gray")
canvas.create_text(100, 20, text=f"Comparisons: {comparisons}", fill="white", font=("Arial", 14))
canvas.create_text(300, 20, text=f"Swaps: {swaps}", fill="white", font=("Arial", 14))
root.update()
time.sleep(0.05)
def bubble_sort(data):
comparisons = 0
swaps = 0
for i in range(len(data) - 1):
has_swapped = False
for j in range(len(data) - 1 - i):
draw_data(data, [j, j+1], -1, comparisons, swaps)
comparisons += 1
if data[j] > data[j+1]:
# Pythonic swap
data[j], data[j+1] = data[j+1], data[j]
has_swapped = True
swaps += 1
if not has_swapped:
break
draw_data(data, [], -1, comparisons, swaps)
def selection_sort(data):
comparisons = 0
swaps = 0
for i in range(len(data)):
min_index = i
for j in range(i+1, len(data)):
draw_data(data, [j], min_index, comparisons, swaps)
comparisons += 1
if data[j] < data[min_index]:
min_index = j
if i != min_index:
data[i], data[min_index] = data[min_index], data[i]
swaps += 1
draw_data(data, [i], min_index, comparisons, swaps)
draw_data(data, [], -1, comparisons, swaps)
def insertion_sort(data):
comparisons = 0
swaps = 0
for i in range(1, len(data)):
j = i
while j > 0:
draw_data(data, [j, j-1], i, comparisons, swaps)
comparisons += 1
if data[j-1] > data[j]:
data[j], data[j-1] = data[j-1], data[j]
swaps += 1
j -= 1
else:
break
draw_data(data, [], -1, comparisons, swaps)
while True:
try:
enter = int(input("""What Algorithm would you like to visualise:
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
Enter: """))
if enter in [1, 2, 3]:
break
else:
print("\nPlease enter a valid choice (1, 2, or 3).\n")
except ValueError:
print("\nInvalid input! Please enter a number.\n")
data = [random.randint(20, 400) for _ in range(30)]
# UI Initialization
root = tk.Tk()
root.title("Algorithm Visualizer")
root.geometry("800x500")
root.config(bg="black")
canvas = tk.Canvas(root, width=800, height=500, bg="black", highlightthickness=0)
canvas.pack()
draw_data(data, [], -1)
time.sleep(1.5)
if enter == 1:
bubble_sort(data)
elif enter == 2:
selection_sort(data)
elif enter == 3:
insertion_sort(data)
root.mainloop()