menu_book Course Curriculum (Intro to Programming) expand_more
Core Concepts
Data Structures
Problem Solving
Binary search
Learn Binary search in Intro to Programming with a focused explanation, a topic-specific example, and a mastery check.
12 min read
•
Intermediate
intro-programming-023.py
python
def binary_search(a,target):
lo,hi=0,len(a)-1
while lo<=hi:
mid=(lo+hi)//2
if a[mid]==target:return mid
if a[mid]<target:lo=mid+1
else:hi=mid-1
return -1
print(binary_search([2,4,7,9,12],9))
What to watch
Content source & attribution
Original Syntaxis Content · Original Syntaxis Content