Skip to content

Commit 76c0074

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#464 from rcode11/patch-1
Create binarySearch.py
2 parents 459138d + 7305253 commit 76c0074

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

binarySearch.py

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

0 commit comments

Comments
 (0)