Skip to content

Commit b0d930b

Browse files
Merge pull request #19 from bhanupavank/patch-3
Create Binarysearch.py
2 parents 5ad4fb5 + 7c03125 commit b0d930b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Binarysearch.py

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

0 commit comments

Comments
 (0)