Skip to content

Commit 071820b

Browse files
authored
Create APP.PY
0 parents  commit 071820b

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

APP.PY

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import tkinter as tk
2+
from tkinter import messagebox
3+
import time
4+
5+
def setup_window():
6+
root = tk.Tk()
7+
root.title("Binary Search Algorithm Visualizer")
8+
root.geometry("600x400")
9+
return root
10+
11+
def create_input_fields(root):
12+
tk.Label(root, text="Enter numbers (comma separated):").pack(pady=5)
13+
array_entry = tk.Entry(root, width=50)
14+
array_entry.pack(pady=5)
15+
16+
tk.Label(root, text="Enter target number:").pack(pady=5)
17+
target_entry = tk.Entry(root, width=20)
18+
target_entry.pack(pady=5)
19+
20+
return array_entry, target_entry
21+
22+
def create_buttons(root, start_search):
23+
search_button = tk.Button(root, text="Start Search", command=start_search)
24+
search_button.pack(pady=10)
25+
26+
return search_button
27+
28+
def display_array(canvas, array, left, right, mid):
29+
canvas.delete("all")
30+
width = 500 / len(array)
31+
for i, num in enumerate(array):
32+
x0 = i * width
33+
x1 = (i + 1) * width
34+
color = "white"
35+
if i == mid:
36+
color = "green"
37+
elif i >= left and i <= right:
38+
color = "yellow"
39+
canvas.create_rectangle(x0, 50, x1, 100, fill=color)
40+
canvas.create_text((x0 + x1) / 2, 75, text=str(num), fill="black")
41+
canvas.update()
42+
43+
def binary_search_visualize(array, target, canvas):
44+
left, right = 0, len(array) - 1
45+
while left <= right:
46+
mid = (left + right) // 2
47+
display_array(canvas, array, left, right, mid)
48+
time.sleep(1) # Pause to visualize the step
49+
if array[mid] == target:
50+
messagebox.showinfo("Result", f"Target {target} found at index {mid}")
51+
return
52+
elif array[mid] < target:
53+
left = mid + 1
54+
else:
55+
right = mid - 1
56+
messagebox.showinfo("Result", f"Target {target} not found in the array")
57+
58+
def start_search(array_entry, target_entry, canvas):
59+
array_str = array_entry.get()
60+
target_str = target_entry.get()
61+
62+
if not array_str or not target_str:
63+
messagebox.showwarning("Input Error", "Please enter both array and target.")
64+
return
65+
66+
try:
67+
array = list(map(int, array_str.split(',')))
68+
except ValueError:
69+
messagebox.showerror("Input Error", "Please enter a valid array of integers.")
70+
return
71+
72+
try:
73+
target = int(target_str)
74+
except ValueError:
75+
messagebox.showerror("Input Error", "Please enter a valid target integer.")
76+
return
77+
78+
array.sort() # Binary search requires a sorted array
79+
binary_search_visualize(array, target, canvas)
80+
81+
def main():
82+
root = setup_window()
83+
84+
array_entry, target_entry = create_input_fields(root)
85+
86+
canvas = tk.Canvas(root, width=500, height=150, bg="white")
87+
canvas.pack(pady=20)
88+
89+
start_search_callback = lambda: start_search(array_entry, target_entry, canvas)
90+
create_buttons(root, start_search_callback)
91+
92+
root.mainloop()
93+
94+
if __name__ == "__main__":
95+
main()

0 commit comments

Comments
 (0)