F.3 Computer Literacy Python Coding - Lesson 9: Range
F.3 Computer Literacy Python Coding - Lesson 9: Range
3 Computer Literacy
Python coding - Lesson 9
Objectives:
● Linear Search algorithm
● Binary Search algorithm on sorted data
Concept
Given a list of data, one common task is to locate the data in the list that satisfies some conditions.
e.g. Consider a list of tuples, where each element of the list has the components (User ID, Age,
Height) as in the case of the last assignment:
One might want to find the Age and Height of a user with a given User ID, say 1491639. This is
searching.
Linear Search
If the list of data has no specific order, as the above example has shown, then the searching must be
done by examining each element one by one, usually starting from the first element of the list, until
either the target is found, or the end of the list is reached. This method is called linear search.
The average no. of elements to be examined before finding the target is N / 2 when the list has N
elements.
e.g. To find the data with User ID = 1491639, one must make 6 comparisons when the first 6 items
are checked to find the required data at alst[5] with the following linear search code:
## Linear Search ##
targetID = 1491639
targetIdx = None # a way to tell if it is not found
for i in range(len(alst)):
if alst[i][0] == targetID:
targetIdx = i
break
if targetIdx != None:
print("ID:", targetID)
print("Age:", alst[targetIdx][1])
print("Height:", alst[targetIdx][2])
else:
print("Not found")
(Note: A search operation might fail. When the target is not inside the list, the target position can be
set to None as a way to indicate this situation)
If the size of the list is large (e.g. with millions of elements), then the comparisons to make would be
huge on average and the linear search method would be highly inefficient.
Binary search
If the set of data is sorted, there is a faster way to find the target.
The basic idea is to keep track of the range of index of the elements to search in each iteration, and
narrow down the range if the middle element of the range does not match the target.
At first, we know that the target should be somewhere between a[0] and a[6] (inclusive)
Now, 41 > a[3] (=23), so we know that the target must be on the right (a[4] to a[6])
The range to search is reduced by half in the next round.
The above steps of calculating mid and comparing a[mid] with target is repeated:
As 18 < a[3], the target is on the left. Use end = mid-1 , same start in the next round.
As 18 > a[1], the target is on the right. Use start = mid+1 , same end in the next round.
As 18 < a[2], the target is on the left. Use end = mid-1, same start in the next round.
But this is not a valid range! We should always have start <= end.
Whenever you have start > end, it indicates that the target is not inside the list.
In Python, we can implement the search as a recursive function, with the last two steps (5 and 6) as
recursive function calls.
To make it slightly more convenient to use, we can write another function to call bsearch with the initial
start and end values:
Exercise
In this exercise, we will reuse the files in the last assignment modify the content as follows:
T2_Ex4_class_class no_mod.py :
1. Copy your last assignment module file T2_Ex3_class_class no_mod.py and save it as
T2_Ex4_class_class no_mod.py (with your actual class and class no in the filename)
2. Add a bsearchID function to the module file that searches for a tuple in the list of tuples where
the first component of the tuple (User ID) matches the targetID.
Hint: just need to make the following changes to the bsearch example:
alst to tplst
alst[mid] to tplst[mid][0]
target to targetID
bsearch to bsearchID (in the recursive calls)
T2_Ex4_class_class no_usemod.py :
4. Copy your last assignment module file T2_Ex3_class_class no_usemod.py and save it as
T2_Ex4_class_class no_usemod.py (with your actual class and class no in the filename)
5. Modify the module import line to import the Ex4 mod file:
6. Remove the code that shows the upper-quartile of each sorting result. You just need to sort
the list of tuples according to the User ID.
7. Add code to ask the user for an input of user ID to search (remember to convert the input into
integer). Call the binarySearchID function in the module file to search and print the results.
Repeat this process until the user has entered 0 for the user ID to search.
8. Submit a zip file of the two .py files using the name T2_Ex4_class_class no.zip with your
actual class and class no in the filename.