menu_book Course Curriculum (Data Structures & Algorithms) expand_more
Complexity & Foundations
Arrays & Strings
Linked Lists
Stacks & Queues
Hashing
Trees & Heaps
Graphs
Logarithmic time
Learn Logarithmic time in Data Structures & Algorithms with a focused explanation, a topic-specific example, edge guidance, and a mastery check.
12 min read
•
Beginner
data-structures-algorithms-005.py
python
def binary_search(a,x):
lo,hi=0,len(a)-1
while lo<=hi:
mid=(lo+hi)//2
if a[mid]==x:return mid
lo,hi=(mid+1,hi) if a[mid]<x else (lo,mid-1)
return -1
print(binary_search([1,4,7,9,12,18],12))
What to watch
Content source & attribution
Original Syntaxis Content · Original Syntaxis Content