Skip to content

Commit f81ee82

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#2 from SaiAshish2906/master
Added Binary Search File
2 parents 34bf722 + 725d943 commit f81ee82

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

binary_search.py

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

0 commit comments

Comments
 (0)