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

Sign up
Appearance settings

Commit 94adda5

Browse files
Create binarySearchRecursive.py
Recursive Binary Search
1 parent ed763c7 commit 94adda5

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎binarySearchRecursive.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def binary_search(arr, x):
2+
low = 0
3+
high = len(arr) - 1
4+
mid = 0
5+
6+
while low <= high:
7+
8+
mid = (high + low) // 2
9+
10+
if arr[mid] < x:
11+
low = mid + 1
12+
13+
14+
elif arr[mid] > x:
15+
high = mid - 1
16+
17+
18+
else:
19+
return mid
20+
21+
22+
return -1
23+
24+
25+
26+
arr = [ 2, 3, 4, 10, 40 ]
27+
x = 10
28+
29+
30+
result = binary_search(arr, x)
31+
32+
if result != -1:
33+
print("Element is present at index", str(result))
34+
else:
35+
print("Element is not present in array")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /