← VCE Software Development U3O1

Binary search in Python

The binary search loop: while low <= high, check the middle item of a sorted list, then move low or high to halve the search range each time.

Understanding Binary search in Python in VCE Software Development U3O1

What it does: Setting low to mid + 1 throws away the lower half, which halves the search range. The loop keeps going while low is less than or equal to high, because that means there are still items left to check. A list index must be a whole number. // gives a whole number, but / gives a float like 2.5 and causes an error. If the middle item is too small, the target must be in the upper half. Moving low up past mid discards the lower half. Python list indexes start at 0 and the last index is len(numbers) - 1. Using len(numbers) would go past the end of the list.

Common mistakes: The loop must keep running while low is less than or equal to high. With >= the loop will not run properly. It should be while low <= high:. Binary search must start at the first index, which is 0. Starting low at 1 skips the first item in the list. It should be low = 0.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 142–155) and word search (Modules 142–166).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which list can a binary search work on?
[14, 2, 20, 5, 9] (unsorted)[2, 5, 9, 14, 20] (sorted)
Matches
while low < high:
↓
Using < instead of <= means the loop stops before checking the last item when low equals high. It should be while low <= high:
Applying
Meanings
Term
while low <= high:
Meaning
The binary search loop: while low <= high, check the middle item of a sorted list, then move low or high to halve the search range each time.
Practice
def binary_search(items, target):
Testing
Gaps
= len(nums) - 1
Check Answer
Spots
3target = 7
4while low >= high:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Starfish' badge — Five-Point Star, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which list can a binary search work on?
A. [14, 2, 20, 5, 9] (unsorted)
B. [2, 5, 9, 14, 20] (sorted) ✓
Binary search only works if the list is sorted. It uses the middle item to decide which half to throw away.
2. Which line correctly keeps the binary search loop running?
A. while low <= high: ✓
B. while low > high:
The loop keeps going while low is less than or equal to high, because that means there are still items left to check.
3. Which line correctly finds the middle index?
A. mid = (low + high) // 2 ✓
B. mid = (low + high) / 2
A list index must be a whole number. // gives a whole number, but / gives a float like 2.5 and causes an error.
4. The item at mid is smaller than the target. What should the code do?
A. low = mid + 1 ✓
B. high = mid - 1
If the middle item is too small, the target must be in the upper half. Moving low up past mid discards the lower half.
5. The item at mid is bigger than the target. What should the code do?
A. low = mid + 1
B. high = mid - 1 ✓
If the middle item is too big, the target must be in the lower half. Moving high down below mid discards the upper half.

Match the Pairs

mid = (low + high) // 2 → Finds the index of the middle item in the current search range
while low <= high: → Keeps looping while there is still a range of items left to search
If the middle item is too small, ignore the left half by moving low up past mid → low = mid + 1
If the middle item is too big, ignore the right half by moving high down below mid → high = mid - 1
while low < high: → Using < instead of <= means the loop stops before checking the last item when low equals high. It should be while low <= high:
low = mid → low never moves past mid, so the loop can get stuck forever on the same range. It should be low = mid + 1

Key Term

while low <= high:
The binary search loop: while low <= high, check the middle item of a sorted list, then move low or high to halve the search range each time.

Fill in the Blanks

1. low = 0 high = len(nums) - 1 while low <= high:
Hint: Type the variable name that holds the index of the last item in the search range.
2. high = len(nums) - 1 while low <= high:
Hint: Type the comparison operator that keeps the loop going while the search range still has at least one item.
3. while low <= high: mid = (low + high) // 2
Hint: Type the operator that divides and rounds down, so mid is a whole-number index.
4. mid = (low + high) // 2 if nums[mid] < target: low = mid + 1
Hint: Type the variable for the middle index. The target is bigger, so the search moves to the right of it.
5. nums = sorted([9, 2, 7, 4]) low = 0
Hint: Type the built-in function that puts the list in order, because binary search only works on a sorted list.

Spot the Error

Question 1
A low = 0
B high = len(nums) - 1
C target = 7
D while low >= high:
E mid = (low + high) // 2
Line D — The loop must keep running while low is less than or equal to high. With >= the loop will not run properly. It should be while low <= high:
Question 2
A low = 1
B high = len(nums) - 1
C while low <= high:
D mid = (low + high) // 2
Line A — Binary search must start at the first index, which is 0. Starting low at 1 skips the first item in the list. It should be low = 0.
Question 3
A low = 0
B high = len(nums) - 1
C mid = (low + high) / 2
D print(nums[mid])
Line C — A single / gives a float like 2.5, which cannot be used as a list index. Use // for whole-number division: mid = (low + high) // 2.
Question 4
A if nums[mid] == target:
B return mid
C elif nums[mid] < target:
D low = mid - 1
E else:
F high = mid - 1
Line D — If the middle item is smaller than the target, the target must be in the upper half, so low must move up. It should be low = mid + 1.
Question 5
A nums = [12, 3, 25, 8, 16]
B nums.reverse()
C low = 0
D high = len(nums) - 1
Line B — Binary search only works on a sorted list. reverse() just flips the order and does not sort it. It should be nums.sort().

Student with a class code? Join your class →