-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
98 lines (76 loc) · 3.62 KB
/
main.py
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
import tkinter as tk
from tkinter import font as tkFont
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
class BubbleSort:
def __init__(self):
self.root = tk.Tk()
self.root.title("Bubble Sort Visualization")
# Üst bilgi (Header)
header_font = tkFont.Font(family="Helvetica", size=12, weight="bold")
self.header_label = tk.Label(self.root, text="Bubble Algorithm by Mehmet Kahya", font=header_font, pady=10)
self.header_label.pack()
# Üst kısım: Kullanıcı girişi ve düğme
self.entry_frame = tk.Frame(self.root)
self.entry_frame.pack(pady=10)
self.entry_label = tk.Label(self.entry_frame, text="Enter numbers separated by commas:")
self.entry_label.pack()
self.entry = tk.Entry(self.entry_frame)
self.entry.pack()
self.button = tk.Button(self.entry_frame, text="Sort", command=self.print_result)
self.button.pack(pady=5)
# Orta kısım: Sonuç etiketi ve grafikler
self.result_frame = tk.Frame(self.root)
self.result_frame.pack(pady=10)
self.result_label = tk.Label(self.result_frame, text="")
self.result_label.pack()
self.figure = Figure(figsize=(6, 6), dpi=100)
self.plot1 = self.figure.add_subplot(211)
self.plot2 = self.figure.add_subplot(212)
self.canvas = FigureCanvasTkAgg(self.figure, master=self.result_frame)
self.canvas.get_tk_widget().pack()
self.toolbar = NavigationToolbar2Tk(self.canvas, self.result_frame)
self.toolbar.update()
self.canvas.get_tk_widget().pack()
# Alt kısım: Footer
self.footer_frame = tk.Frame(self.root)
self.footer_frame.pack(side=tk.BOTTOM, fill=tk.X)
bold_font = tkFont.Font(family="Helvetica", size=10, weight="bold")
self.footer_label = tk.Label(self.footer_frame, text="Bubble Sort Visualization - © 2024 Mehmet Kahya", bd=1, relief=tk.SUNKEN, anchor=tk.CENTER, font=bold_font)
self.footer_label.pack(side=tk.BOTTOM, fill=tk.X)
def sort(self, arr):
n = len(arr)
swaps = 0
for i in range(n):
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swaps += 1
return arr, swaps
def print_result(self):
arr = [int(num) for num in self.entry.get().split(',')]
sorted_arr, swaps = self.sort(arr[:])
self.result_label.config(text=f"First List: {arr}\nSorted List: {sorted_arr}\nNumber of swaps: {swaps}")
self.plot1.clear()
self.plot1.bar(range(len(arr)), arr, color='r')
self.plot1.set_title('Unsorted Array')
self.plot1.set_xticks(range(0, len(arr), max(1, len(arr)//10)))
self.plot1.set_yticks(range(min(arr), max(arr) + 1, max(1, (max(arr)-min(arr))//10)))
self.plot1.set_xlabel('Index')
self.plot1.set_ylabel('Value')
self.plot2.clear()
self.plot2.bar(range(len(sorted_arr)), sorted_arr, color='g')
self.plot2.set_title('Sorted Array')
self.plot2.set_xticks(range(0, len(sorted_arr), max(1, len(sorted_arr)//10)))
self.plot2.set_yticks(range(min(sorted_arr), max(sorted_arr) + 1, max(1, (max(sorted_arr)-min(sorted_arr))//10)))
self.plot2.set_xlabel('Index')
self.plot2.set_ylabel('Value')
self.figure.tight_layout()
self.canvas.draw()
def run(self):
self.root.mainloop()
def main():
bubble_sort = BubbleSort()
bubble_sort.run()
if __name__ == "__main__":
main()