Linear Search (OCR A-Level Computer Science): Revision Notes
📚 Revision Notes
Linear Search
Overview
Linear Search is a simple algorithm used to find the position of a target value in a list. It works by sequentially checking each element of the list until the target is found or the end of the list is reached. Linear Search is easy to implement but can be inefficient for large datasets.
How Linear Search Works
- Process:
- Start at the first element of the list.
- Compare each element with the target value.
- If a match is found, return the position of the element.
- If the end of the list is reached without finding the target, return an indication that the target is not in the list.
- Applicable to:
- Both unsorted and sorted datasets.
Time Complexity
- Best Case: O(1) (target is the first element).
- Worst Case: O(n) (target is the last element or not present).
- Average Case: O(n/2), simplified to O(n).
Space Complexity
- O(1), as no extra memory is required beyond the input list.
infoNote
Example: Linear Search
Given the array:
3, 7, 10, 15, 18, 20
Target: 15
Step-by-Step Execution:
- Compare 3 with 15 → Not a match.
- Compare 7 with 15 → Not a match.
- Compare 10 with 15 → Not a match.
- Compare 15 with 15 → Match found. Position: Index 3.
Pseudocode for Linear Search
METHOD LinearSearch(array, target)
FOR i FROM 0 TO array.length - 1
IF array[i] = target
RETURN i // Target found at index i
ENDFOR
RETURN -1 // Target not found
Python Example
def linear_search(array, target):
for i in range(len(array)):
if array[i] == target:
return i # Target found
return -1 # Target not found
# Example usage:
data = [3, 7, 10, 15, 18, 20]
print(linear_search(data, 15)) # Output: 3
print(linear_search(data, 5)) # Output: -1
Tracing Linear Search
infoNote
Example
Given the array:
2, 5, 8, 12, 16, 23, 38
Target: 23
- Iteration 1: Compare 2 with 23 → No match.
- Iteration 2: Compare 5 with 23 → No match.
- Iteration 3: Compare 8 with 23 → No match.
- Iteration 4: Compare 12 with 23 → No match.
- Iteration 5: Compare 16 with 23 → No match.
- Iteration 6: Compare 23 with 23 → Match found. Output: Index 5.
Note Summary
infoNote
Common Mistakes
- Not Handling Target Not Found: Forgetting to return a special value (e.g., -1) when the target is not in the list.
- Index Errors: Incorrect loop bounds can cause out-of-range errors.
- Confusion Between Return and Print: Using print instead of return inside the function can lead to incorrect handling of results in larger programmes.
- Inefficient for Large Datasets: Using Linear Search on very large lists when the data could be sorted and searched more efficiently with algorithms like Binary Search.
infoNote
Key Takeaways
- Linear Search is a simple and versatile algorithm that works on both sorted and unsorted datasets.
- It has a time complexity of O(n), making it inefficient for large datasets.
- The algorithm is straightforward to implement and understand.
- Ensure proper handling of cases where the target value is not found.