Visualize Linear Search
Watch how Linear Search checks each item one by one. Click "Next Step" to see the search in action.
Target: ?
Click 'Next Step' to begin.
Identify Code Parts
This simple function performs a linear search. Click on the highlighted parts to identify their role.
def linear_search(data, target):
for index in range(len(data)): # Check each item
if data[index] == target:
return index # Found it! Return position
return -1 # Not found after checking all
Identify all 4 key parts!
Predict the Steps
How many comparisons (checks) will linear search need to find the target (or know it's not there)? Enter your prediction. This relates to *efficiency*.
Simple Code Check
Let's practice a related concept. Use Python's `in` operator to check if a value exists in a list.
in
operator to check if the number 42
is present in the list [10, 20, 42, 55]
. Print the result (which will be `True` or `False`).
Conceptual Check-in
Think about how linear search works and its efficiency. Answer the questions below briefly.
Question 1: Best Case
When does linear search perform the *fastest* (best-case scenario)? Briefly describe.
Question 2: Worst Case
When does linear search perform the *slowest* (worst-case scenario)? Briefly describe.
Question 3: Sorted Data
Does linear search become faster if the list is already sorted? Why or why not?
Module 27 Practice Complete!
Great work exploring Linear Search! You've visualized it, analyzed its code, and thought about its efficiency.
Ready for more? Tackle the Advanced Practice for Module 27!