-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.py
More file actions
36 lines (30 loc) · 1.42 KB
/
binary_search.py
File metadata and controls
36 lines (30 loc) · 1.42 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
#############################################################
###################### Binary Search ########################
#############################################################
# Binary Search Function :: Created by Us Manually.
# value == parameter of our function , for the value to search .
# database == from where our value will be searched
# No of Steps are based on the Algorith Log base n .
print("""#############################################################
###################### Binary Search ########################
#############################################################""")
database=list(range(1,12))
def binary_search(database,value):
start=0
end=len(database) -1
middle=int((start + end) / 2)
# while not here is used beacause we dont want our program running infinetly so whenever it gets true it will convert itself to false.
while not (database[middle] == value) and start <= end:
if value > database[middle]:
start = middle + 1
else:
end = middle - 1
middle=int((start + end) / 2)
print("start :",start , "End :",end ,"Middle :",middle)
if database[middle] == value:
print("We Found the Value at ",middle)
else:
print("not found")
print("start :",start , "End :",end ,"Middle :",middle)
value = int(input("Enter the Value for Search in Database : "))
binary_search(database,value)