Last active
February 10, 2020 14:24
-
-
Save manojnaidu619/21ccbad4eeff2707ee129afc270ca4f6 to your computer and use it in GitHub Desktop.
Binary Search in Python(Iterative version)
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(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