-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.py
More file actions
38 lines (33 loc) · 688 Bytes
/
binary.py
File metadata and controls
38 lines (33 loc) · 688 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
36
37
38
# arr = [1,2,3,45,78,888]
# x = len(arr)
# n = int(input("enter the number :"))
# def binary(arr,n):
# low = 0
# high = x -1
# while low <= high :
# mid = (low+ high)//2
# if arr[mid] == n :
# return mid
# elif arr[mid] < n :
# low = mid + 1
# else :
# high = mid - 1
# return -1
# print(binary(arr,n))
# for binary search array should be sorted
arr = [1,2,3,4,56]
x = len(arr)
n = int(input("enter the number :"))
low = 0
high = x -1
for i in range (x):
mid = (low+high)//2
if arr[mid] == n :
print("no found",mid)
break
elif arr[mid] < n :
low = mid + 1
else :
high = mid - 1
else :
print("element not found")