Skip to content
This repository was archived by the owner on Oct 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #156 from mahesdwivedi/master
Browse files Browse the repository at this point in the history
binary search using python
  • Loading branch information
ankitjena authored Oct 8, 2018
2 parents 75fb9c7 + 2d73a21 commit ee1259c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions search/binary_search/python/binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def binarySearch(arr, l, r, x):

while l <= r:

mid = l + (r - l)//2;


if arr[mid] == x:
return mid


elif arr[mid] < x:
l = mid + 1


else:
r = mid - 1



return -1



arr = [ 2, 3, 4, 10, 40 ]
x = 10


result = binarySearch(arr, 0, len(arr)-1, x)

if result != -1:
print ("Element is present at index ",result)
else:
print ("Element is not present in array")

0 comments on commit ee1259c

Please sign in to comment.