8000 add python script binary-search in data-structures · Chaos-19/python-algorithm-@741d7e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 741d7e3

Browse files
hoseinfzadabranhe
authored andcommitted
add python script binary-search in data-structures
1 parent 965d88c commit 741d7e3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

data-structures/binarySerach.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def binSearch(a, x, low, high):
2+
#Return True if target is found in indicated portion of a Python list.
3+
#The search only considers the portion from data[low] to data[high] inclusive.
4+
5+
if low > high:
6+
return False # interval is empty; no match
7+
else:
8+
mid = (low + high) // 2
9+
if x == a[mid]: # found a match
10+
return True
11+
elif x < a[mid]:
12+
# recur on the portion left of the middle
13+
return binSearch(a, x, low, mid - 1)
14+
else:
15+
# recur on the portion right of the middle
16+
return binSearch(a, x, mid + 1, high)
17+
a = [5, 10, 15, 20, 25, 30, 40]
18+
x = 20
19+
low = 0
20+
high = 6
21+
result = binSearch(a, x, low, high)
22+
if result:
23+
print("The value ", x, " Found")
24+
else:
25+
print("The value ", x, " Not found")

0 commit comments

Comments
 (0)
2940
0