Establish the contract before tracing
When the brief asks you to search a list, find out whether it wants a Boolean, a position or the matching record. These are different outputs. If a name appears twice, does the task ask for the first match or all matches? A linear search can stop at the first match. On [8, 2, 8, 4], searching for 8 returns index 0 if the function promises the first index. Searching for 4 visits four values and returns index 3. The index and the number of comparisons are different quantities.
An empty list contains no valid positions. A search should report absence without accessing position 0. -1 is a useful absence result if all successful positions are non-negative, but do not then use it as a list index without checking. In Python values[-1] selects the last item, so forgetting that guard could display a real record when no record was found.
Trace binary search on an absent target
Suppose the sorted data is [2, 4, 7, 10, 13] and the target is 8. Record the candidate interval using inclusive indices. The midpoint is (low + high) // 2, using integer division. When the middle value is below the target, move low to middle + 1. The middle has already been checked, so keeping it could prevent progress.
| Low | High | Middle index | Middle value | New interval |
|---|---|---|---|---|
| 0 | 4 | 2 | 7 | 3 to 4 |
| 3 | 4 | 3 | 10 | 3 to 2, which is empty |
The target is absent. In each pass the interval shrinks. Starting with high = len(values) would sometimes allow the search to look beyond the last index. Using while low < high would skip the final candidate when low equals high. Use a one-item list as a deliberate test of both issues. On an unsorted list, the rule that everything on one side is too large or too small does not hold.
Trace two kinds of sorting
Bubble sort looks at adjacent items and swaps when the left one is greater. One pass through [5, 3, 2, 4] gives [3, 2, 4, 5]. The final 5 is settled, but the rest is not sorted yet. Repeat on the unsorted prefix. It is possible to finish early when a full pass makes no swaps. If you use a flag for this, reset it at the start of each pass and set it when you swap. Then test an already sorted list and one in reverse order.
Merge sort has a different structure. Split a list into halves until each sublist contains at most one item. Merge sorted sublists by comparing the first unmerged item on each side. For [3, 8] and [1, 5], take 1, then 3, then 5. The second sublist is empty, so append the remaining 8. Merging two sorted lists preserves order, which is what makes the method work when the small merges are combined into larger ones. Trace the split and merge of [8, 3, 5, 1] before trying to implement it.
Compare algorithms with evidence
A linear search can find the first item in one comparison, but a missing target can require inspecting every item. Binary search reduces the candidate interval each time, but the list must already be sorted. Sorting only for one lookup can cost more work than scanning once. For many lookups on stable data, the up-front sort can make sense. In your explanation name the operation you counted and the assumptions about the input.
Practise a Paper 2 change request
An inventory file stores a list of integer codes. A starter search uses while low < high with an inclusive upper bound. Test [7] with target 7, then use [2, 4, 7, 10, 13] with target 13 and 8. Change the condition and explain why your tests expose the mistake. In the sandbox, place the codes on one line and the target on the next. Use the editor's manual run for this separate binary-search task.