Concept
Binary search halves the search range each step by comparing with the middle element, finding a value in a sorted array in O(log n). It needs the array sorted and the comparison consistent.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Search on sorted arrays doing its job.
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 -1Setting lo = mid or hi = mid instead of mid + 1 or mid - 1. The range then may not shrink, and the loop runs forever on inputs where the target is absent.
Your roadmap puts these before Search on sorted arrays, unless you already know them.
Once a session shows you understand Search on sorted arrays, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.
Topics that build on Search on sorted arrays.