Hunt a value in a list the straightforward way and the halving way — then count the comparisons each one spends.
01
Learning objectives
Write and trace a linear search that returns the index of a target or -1.
Write and trace a binary search on a sorted list, updating low, high and mid.
Count and compare the comparisons both searches spend on the same data.
Explain why binary search requires the precondition that the list is sorted.
Predict the best and worst cases for each search on a given list.
02
Lesson notes
Searching answers one question: at what position does this value sit? The honest, always-works method is linear search: compare the target with element 0, then 1, then 2, and so on, returning the index on a match or −1 at the end. It makes no assumptions about the data, which is its strength; its cost is that a miss forces it to look at every single element.
Binary search demands a price first: the list must be sorted. Then it plays the dictionary game. Look at the middle element: if it equals the target, you are done; if the target is larger, the entire lower half is eliminated at once; if smaller, the upper half goes. Maintain low and high as the current search window, compute mid = (low + high) // 2, and shrink the window until it is empty.
The payoff is enormous. On 1,000,000 sorted values, linear search may need 1,000,000 comparisons, but binary search needs at most about 20, because each comparison halves what remains: 1,000,000 to 500,000 to 250,000, down to 1 in roughly 20 halvings. Twenty questions suffice to find one value among a million — that is the power of halving. It is why the telephone directory, the Urdu dictionary and every database index work the binary way.
The precondition is where marks are won and lost: binary search on an unsorted list does not crash — it silently returns wrong answers or misses values that exist. That silent failure is worse than a crash, so the first question to ask about any binary search code is: who guarantees the list is sorted? In exams, state the precondition explicitly before you trace anything.
Board tip: build a small trace table with columns low, high, mid and nums[mid] for each round. Examiners award method marks for a correct mid computation and window update even when the arithmetic slips at the end. Also know the built-ins: x in data tests membership, and data.index(x) returns the position of the first match, raising ValueError if there is none.
Binary search halves the search space
03
Worked code examples
Linear search, found and not found
The loop checks positions in order; 5 sits at index 3, and 50 is absent, so the sentinel -1 reports failure after all five checks.
def linear_search(nums, target):
for i in range(len(nums)):
if nums[i] == target:
return i
return -1
data = [34, 12, 78, 5, 91]
print(linear_search(data, 5))
print(linear_search(data, 50))
Output
3
-1
Binary search on a sorted list
For 23 the very first middle element (index 3) matches. For 44 the window shrinks to the right — low becomes 4, and then mid 5 holds 44.
in answers presence without giving a position, and index() returns the position of the first match — raising ValueError if there is none.
data = [34, 12, 78, 5, 91]
print(5 in data, 50 in data)
print(data.index(78))
Output
True False
2
04
Key terminology
linear search
Checking a list element by element from the start until the target is found or the list ends.
binary search
Repeatedly halving a sorted list by comparing the target with the middle element.
target
The value a search is looking for.
precondition
A requirement that must hold before an algorithm runs — binary search demands sorted data.
sentinel
A conventional not-found signal, -1, chosen because valid indexes start at 0.
05
Common misconceptions
Common mistake
Binary search is always the better search.
Correct understanding
Only when the list is already sorted. On unsorted data linear search is the only correct choice, and for one lookup on a tiny list the difference is negligible.
Common mistake
Binary search fails politely if the list is unsorted.
Correct understanding
It runs silently and can return -1 for a value that exists, or a wrong position — the dangerous kind of bug.
Common mistake
Returning -1 for not found is a bug because -1 is a valid index.
Correct understanding
As a search result, -1 is a deliberate sentinel: no positive index is valid for failure, so the signal is unambiguous.
06
Check your understanding
1. data = [2, 4, 6, 8, 10, 12]. Binary search for 10 — what index is returned?
2. What is the worst case for linear search on a 100-element list?
3. A marks register is not sorted. Which search may you use on it?