forked from sumedha3111/Python-for-Beginners
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysearch.py
More file actions
Latest commit
21 lines (21 loc) · 575 Bytes
/
Copy pathbinarysearch.py
File metadata and controls
21 lines (21 loc) · 575 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
defbinary_search(arr, x):
low=0
high=len(arr) -1
mid=0
whilelow<=high:
mid= (high+low) //2
ifarr[mid] <x:
low=mid+1
elifarr[mid] >x:
high=mid-1
else:
returnmid
return-1
arr=list(map(int, input("Enter a multiple values: ").split()))
arr.sort()
x=int(input("enter the number to be found : "))
result=binary_search(arr, x)
ifresult!=-1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")