Brute force, bisection or Python's in: which search barely notices a tenfold bigger list, and is the built-in one really a better algorithm?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Brute force walks the list from the beginning to the end and asks each element a single question: is this the one? Write that loop yourself rather than leaning on the built-in: one check passes a list that raises an error if you ask it x in L, although a for loop over it works normally. Define is_in(L, x), returning True the moment an element equals x and False once the loop runs off the end.
is_in, binary_search and the one-liner x in L
Copy is_in and binary_search, note that the second needs a sorted list, and hand-trace it on a short one, recording lo, hi and mid as each pass halves the range.
Averaging over the first, middle and last element
Write the timing loop that searches for L[0], L[len(L)//2] and L[-1] and divides the total elapsed time by three, recording that these span the cheap, middling and most expensive cases.
Tenfold input: linear searches versus the factor 1.26
Tabulate the 10-million and 100-million times, marking is_in and in as linear, binary_search's factor of about 1.26 as neither constant nor linear, and noting the five-orders-of-magnitude gap between brute force and bisection.
Built-in in against the hand-written loop
Compute the ratio of is_in's time to in's at both large sizes, roughly three, and record the conclusion: a better constant factor, the same linear scaling, not a better algorithm.