forked from tushargoyal02/pythonDsa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.py
More file actions
35 lines (23 loc) · 743 Bytes
/
binarySearch.py
File metadata and controls
35 lines (23 loc) · 743 Bytes
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
#!/usr/bin/python3
def binaryfunc(arr, l, r, x):
while l<=r:
#finding the mid values
mid = int(l+(r-l) / 2)
#print(mid)
#print(type(mid))
#checking condition if mid value is equal to search value or not
if(arr[mid] == x):
return mid
#if not and value is greater than mid value ignore left part and increase its value
elif arr[mid] < x:
l = mid+1
# if value less than mid value ignore right part and decrease right value by -1
else:
r= mid-1
return -1
#declaring an array
arr=[1,2,3,4,5]
r=len(arr)-1
x=2
result = binaryfunc(arr,0, r, x)
print("the value for the result is:", result)