Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Created February 10, 2020 14:24
Show Gist options
  • Save manojnaidu619/e69ced5746d26f17b86c00ca5a6b3d6d to your computer and use it in GitHub Desktop.
Save manojnaidu619/e69ced5746d26f17b86c00ca5a6b3d6d to your computer and use it in GitHub Desktop.
Binary Search in Python(Recursive version)
def binarySearch(arr, key, low, high):
if low>high:
return False
else:
mid = (low+high)/2
if arr[mid]==key:
return True
elif key<arr[mid]:
return binarySearch(arr,key,low,mid-1)
else:
return binarySearch(arr,key,mid+1,high)
result = binarySearch([1,2,3,4,5],6,0,4)
if result: print("Element found!")
else: print("Element not found!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment