Last active
July 20, 2019 18:39
-
-
Save moulya-somasundara/ce3067dd2027199cf14b10b2c0733124 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def binarySearch (list, l, r, target): | |
# Check base case | |
if r >= l: | |
mid = l + (r - l)/2 | |
# If element is present at the middle itself | |
if arr[mid] == target: | |
return mid | |
# If element is smaller than mid, then it can only | |
# be present in left half of the list | |
elif arr[mid] > target: | |
return binarySearch(arr, l, mid-1, target) | |
# Else the element can only be present in right half of the list | |
else: | |
return binarySearch(arr, mid+1, r, target) | |
else: | |
# Element is not present in the list | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment