You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#Python-DSA---Simple Binary Search --- works best on sorted list
# Python don't have Arrays, as built-in DS, but list[] as built-in DS, similar to that of array in other languages, like C.
defbinary_search(list, item):
low=0
high=len(list) -1
whilelow<=high:
mid= (low+high) //2# Use integer division to find the middle index --- The mid should be calculated as (low + high) // 2 to find the middle index correctly.
guess=list[mid]
ifguess==item:
returnmid
ifguess>item:
high=mid-1
else:
low=mid+1
return-1# Return -1 to indicate that the item was not found in the list