forked from AllAlgorithms/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySerach.py
More file actions
25 lines (24 loc) · 758 Bytes
/
binarySerach.py
File metadata and controls
25 lines (24 loc) · 758 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
def binSearch(a, x, low, high):
#Return True if target is found in indicated portion of a Python list.
#The search only considers the portion from data[low] to data[high] inclusive.
if low > high:
return False # interval is empty; no match
else:
mid = (low + high) // 2
if x == a[mid]: # found a match
return True
elif x < a[mid]:
# recur on the portion left of the middle
return binSearch(a, x, low, mid - 1)
else:
# recur on the portion right of the middle
return binSearch(a, x, mid + 1, high)
a = [5, 10, 15, 20, 25, 30, 40]
x = 20
low = 0
high = 6
result = binSearch(a, x, low, high)
if result:
print("The value ", x, " Found")
else:
print("The value ", x, " Not found")