Binary Search in Python
Binary search cuts a sorted list in half each time. Look at the middle. Too small? Search the right half. Too big? Left half. About log n steps — O(log n). First sentence in the viva: the list must be sorted. Unsorted binary search is wrong, not “almost right”.
Trace [1, 2, 3, 4, 5] looking for 4: mid 3, go right, find 4. State the sorted precondition before you write a line of code.
Binary Search in Python — output — 3. 12 is at index 3. Unsorted list would be wrong — sort first.
Sorted requirement is mandatory.