-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting Algorithm Visualizer (Bubble, Quick, Merge, etc.).py
More file actions
133 lines (105 loc) · 3.09 KB
/
Sorting Algorithm Visualizer (Bubble, Quick, Merge, etc.).py
File metadata and controls
133 lines (105 loc) · 3.09 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
import tkinter as tk
import random
import time
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
# ---------- Sorting Algorithms ----------
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
yield arr
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr)//2
left = arr[:mid]
right = arr[mid:]
yield from merge_sort(left)
yield from merge_sort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
yield arr
while i < len(left):
arr[k] = left[i]
i += 1
k += 1
yield arr
while j < len(right):
arr[k] = right[j]
j += 1
k += 1
yield arr
def quick_sort(arr, low, high):
if low < high:
pivot = partition(arr, low, high)
yield arr
yield from quick_sort(arr, low, pivot-1)
yield from quick_sort(arr, pivot+1, high)
def partition(arr, low, high):
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i+1
# ---------- GUI & Visualization ----------
def generate_array():
global data
data = [random.randint(10, 100) for _ in range(30)]
draw_data(data)
def draw_data(data):
ax.clear()
ax.bar(range(len(data)), data)
canvas.draw()
def start_sort():
algo = algorithm_menu.get()
if algo == "Bubble Sort":
generator = bubble_sort(data)
elif algo == "Merge Sort":
generator = merge_sort(data)
elif algo == "Quick Sort":
generator = quick_sort(data, 0, len(data)-1)
else:
return
animate(generator)
def animate(generator):
try:
next(generator)
draw_data(data)
root.after(100, lambda: animate(generator))
except StopIteration:
draw_data(data)
# ---------- Main Window ----------
root = tk.Tk()
root.title("Sorting Algorithm Visualizer")
root.geometry("800x600")
root.resizable(False, False)
data = []
# Controls
frame_controls = tk.Frame(root)
frame_controls.pack(pady=10)
algorithm_menu = tk.StringVar()
algorithm_menu.set("Bubble Sort")
tk.OptionMenu(frame_controls, algorithm_menu,
"Bubble Sort", "Merge Sort", "Quick Sort").pack(side=tk.LEFT, padx=10)
tk.Button(frame_controls, text="Generate Array",
command=generate_array).pack(side=tk.LEFT, padx=10)
tk.Button(frame_controls, text="Start Sorting",
command=start_sort).pack(side=tk.LEFT, padx=10)
# Graph
fig, ax = plt.subplots(figsize=(8, 4))
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack()
root.mainloop()