Master programming syntax, solve interactive challenges & run live code in Syntaxis Explore Arena →
menu_book Course Curriculum (Data Structures & Algorithms) expand_more
Complexity & Foundations
Arrays & Strings
Linked Lists
Stacks & Queues
Hashing
Trees & Heaps
Graphs
Sorting Searching & DP
Data Structures & Algorithms / Sorting Searching & DP

Binary search

Learn Binary search in Data Structures & Algorithms with a focused explanation, a topic-specific example, edge guidance, and a mastery check.

12 min read • Advanced
data-structures-algorithms-076.py
python
a=[2,5,8,12,16,23];x=12;lo,hi=0,len(a)
while lo<hi:
    mid=(lo+hi)//2
    if a[mid]<x:lo=mid+1
    else:hi=mid
print(lo if lo<len(a) and a[lo]==x else -1)
What to watch
Content source & attribution
Original Syntaxis Content · Original Syntaxis Content