Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions data-structures/binarySerach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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")