Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save moulya-somasundara/ce3067dd2027199cf14b10b2c0733124 to your computer and use it in GitHub Desktop.
Save moulya-somasundara/ce3067dd2027199cf14b10b2c0733124 to your computer and use it in GitHub Desktop.
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