Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 12007f0

Browse filesBrowse files
hoseinfzadabranhe
authored andcommitted
add python script binary-search in data-structures
1 parent 2a00125 commit 12007f0
Copy full SHA for 12007f0

File tree

Expand file treeCollapse file tree

1 file changed

+25
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+25
-0
lines changed

‎data-structures/binarySerach.py

Copy file name to clipboard
+25Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.