Linear Search Algorithm in DSA Python - Time & Space Complexity
We want to understand how the time taken by linear search changes as the list size grows.
How many steps does it take to find an item or decide it's not there?
Analyze the time complexity of the following code snippet.
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
This code looks through each item in the list one by one to find the target value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element in the list one by one.
- How many times: Up to n times, where n is the number of items in the list.
As the list gets bigger, the number of checks grows roughly the same as the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of steps grows directly with the size of the list.
Time Complexity: O(n)
This means the time to find the target grows linearly with the number of items.
[X] Wrong: "Linear search always takes the same time no matter the list size."
[OK] Correct: The time depends on where the target is or if it is missing; bigger lists usually mean more checks.
Understanding linear search time helps you compare it with faster methods and shows you can analyze simple algorithms clearly.
"What if the list was sorted? How would the time complexity change if you used a different search method?"
Practice
Linear Search Algorithm?Solution
Step 1: Understand linear search process
Linear search checks each element in order, one by one.Step 2: Compare with other search methods
Binary search divides list, hash uses extra memory, linear is simple sequential check.Final Answer:
Check each element one by one until the target is found or list ends -> Option CQuick Check:
Linear search = sequential check [OK]
- Confusing linear search with binary search
- Thinking linear search requires sorted list
- Assuming linear search is faster than binary search always
arr in Python?Solution
Step 1: Identify how to loop over indices
To access elements by index, userange(len(arr)).Step 2: Check each option's correctness
for i in arr: loops over elements, not indices; B is invalid becauserangeneeds an integer; D is invalid syntax.Final Answer:
for i in range(len(arr)): -> Option DQuick Check:
Loop indices = range(len(arr)) [OK]
- Using range(arr) instead of range(len(arr))
- Trying to loop with invalid syntax like 'for i to len(arr)'
- Confusing looping over elements vs indices
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
print(linear_search([4, 2, 7, 1, 3], 7))Solution
Step 1: Trace the loop and compare elements
Check elements: arr[0]=4 (no), arr[1]=2 (no), arr[2]=7 (yes, matches target 7).Step 2: Return index when found
Function returns index 2 immediately when target found.Final Answer:
2 -> Option AQuick Check:
Index of 7 in list = 2 [OK]
- Returning element value instead of index
- Returning -1 even when target is present
- Confusing index with element value
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] = target:
return i
return -1Solution
Step 1: Identify syntax error in condition
The if condition uses '=' which is assignment, not comparison.Step 2: Correct operator for comparison
Use '==' to compare values in if condition.Final Answer:
Using '=' instead of '==' in the if condition -> Option AQuick Check:
Comparison needs '==' not '=' [OK]
- Using single '=' in if condition
- Confusing assignment and comparison operators
- Ignoring syntax errors causing runtime failure
def linear_search_all(arr, target):
positions = []
for i in range(len(arr)):
if arr[i] == target:
# What to do here?
return positions
print(linear_search_all([1, 3, 7, 3, 9, 3], 3))Solution
Step 1: Understand requirement to find all matches
We must collect all indices where target appears, not stop at first.Step 2: Append matching indices to list
Inside if, addpositions.append(i)to store all found positions.Final Answer:
Append index i to positions list inside the if block -> Option BQuick Check:
Collect all indices by appending [OK]
- Returning immediately on first match
- Breaking loop early
- Assigning target to positions instead of appending
Ready to prove it?
Pass the test and this pattern moves unknown
