Skip to content

Instantly share code, notes, and snippets.

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