forked from dunmanhigh/y2math
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.py
More file actions
74 lines (53 loc) · 1.9 KB
/
binary_search.py
File metadata and controls
74 lines (53 loc) · 1.9 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
import tkinter as tk
window = tk.Tk()
window.title("Binary search")
frame = tk.Frame(window)
frame.grid()
def display_arr (arr, row_, mid_index=-1):
for i in range (len(arr)):
if i == len(arr)//2:
tk.Label(frame, text=str(arr[i]), fg="yellow", bg="black").grid(row=row_, column=i+4)
else:
tk.Label(frame, text=str(arr[i]), fg="white", bg="black").grid(row=row_, column=i+4)
def visualbin ():
global frame
frame.destroy()
arr = array.get().split(",")
target = target_.get()
arr = [int(x) for x in arr]
arr.sort()
target = int(target)
curr_row = 10
lower_bound = 0
upper_bound = len(arr)-1
frame = tk.Frame(window)
frame.grid(row=curr_row)
display_arr(arr, curr_row+1)
curr_row += 1
found = False
while lower_bound <= upper_bound:
mid = (lower_bound+upper_bound)//2
tk.Label(frame, text="Mid: "+str(arr[mid]), fg="yellow", bg="black").grid(row=curr_row, column=1)
display_arr(arr[lower_bound:upper_bound+1], curr_row, mid)
if arr[mid] == target:
tk.Label(frame, text="Found!").grid(row=curr_row+2, column=1)
found = True
break
elif arr[mid] > target:
upper_bound = mid-1
else:
lower_bound = mid+1
curr_row += 2
if not found:
tk.Label(frame, text="Not found!").grid(row=curr_row+2, column=1)
tk.Label(window, text="Visualizing binary search", fg="white",
bg="black").grid(row=1)
array = tk.StringVar()
array.set("1,2,3")
target_ = tk.StringVar()
target_.set("0")
tk.Label(window, text="Enter the array (separated by commas): ").grid(row=2)
tk.Entry(window, textvariable=array).grid(row=2, column=1)
tk.Label(window, text="Enter the target: ").grid(row=3)
tk.Entry(window, textvariable=target_).grid(row=3, column=1)
tk.Button(window, text="Confirm entry",command=visualbin).grid(row=4)