Introduction to Linear Search
Linear search, also known as sequential search, is a simple searching algorithm that checks each element of a list in order until a match is found or the entire list has been traversed.
Algorithm Steps
- Start at the beginning of the list.
- Compare the target value with the current element.
- If the target value matches the current element, the search is successful and the position of the element is returned.
- If the target value does not match the current element, move to the next element in the list.
- Repeat steps 2-4 until a match is found or the end of the list is reached.
What is a linear search?
Linear Search Visualisation
Time & Space Complexity
Time complexity - O(n)
Because linear search potentially needs to check each item (n) in the list once in the list, the time complexity is O(n)
Space complexity O(1)
The space complexity of a linear search algorithm is generally O(1), which means it uses a constant amount of additional memory regardless of the size of the input data. This is because a linear search does not require any additional data structures or memory allocation that scales with the size of the input.
You may use a few variables for control and temporary storage, but the memory usage remains constant and doesn't depend on the size of the input data.
What is the worst-case time complexity of linear search?
Pros and Cons of Linear Search
Advantages
- Simple and easy to understand
- Works on both sorted and unsorted lists
- Works well for small data sets
- Useful if you expect the item to be near the beginning of the list
Disadvantages
- Inefficient for large data sets
- Time complexity: O(n), where n is the size of the list
- Can be slow compared to other search algorithms
In which situation is linear search usually the most efficient?
Linear Search on a Sorted List
If the list is sorted, a linear search can stop early as soon as it passes the place where the target would be.
- Stop if the current item equals the target: found.
- Stop if the current item is greater than the target: the target is not in the list.
List: 3, 8, 12, 19, 25 Target: 10 3 < 10 â keep going 8 < 10 â keep going 12 > 10 â stop, not found
Only three items were checked instead of five. The worst case, a target larger than every item, still checks them all, so it is still O(n).
A linear search on the sorted list 4, 9, 15, 21, 30 is looking for 17. Where does it stop?
Sorted Linear Search in Python
This function returns the index of the target, or -1 if it is not found. The elif branch adds the early exit for a sorted list.
def linear_search_sorted(items, target):
for i in range(len(items)):
if items[i] == target:
return i # found: stop early
elif items[i] > target:
return -1 # gone past it: stop early
return -1 # reached the end
numbers = [3, 8, 12, 19, 25]
print(linear_search_sorted(numbers, 19))
print(linear_search_sorted(numbers, 10))3 -1
Without the elif branch, the same function works on an unsorted list, stopping early only when the target is found.
Review: Fill in the Blanks
The search process begins at the first of the list and continues until the target element is found or the end of the list is reached. To perform the search, a between the target element and each element in the list is made. If a match is found, a value indicating the presence or absence of the target element is returned.
One of the key factors in evaluating the of an algorithm is its , which refers to how the algorithm's execution time increases with the size of the input. In the case of Sequential Search, the runtime complexity is linear, commonly denoted as , where n represents the number of elements in the list.
In the , the target element is located at the end of the list or is absent from the list entirely. In such cases, the algorithm's performance is at its poorest, as it has to traverse through all the elements until reaching the end or determining the absence of the target element. Consequently, the worst-case scenario has a of O(n).
Conversely, in the , the target element is found at the first index, resulting in the algorithm's fastest execution. However, even in this case, the algorithm still has to traverse through the list once to confirm the presence of the target element. The best-case scenario still exhibits a linear time complexity of O(n).
The takes into consideration various input distributions. On average, the algorithm would need to search through half of the list, resulting in a linear time complexity of O(n/2). However, in the analysis of algorithms, the average-case scenario is generally simplified to the worst-case scenario. This simplification is done as it provides a conservative estimate of the algorithm's efficiency.
Complete! Ready to test your knowledge?
Linear Search
- Introduction to Linear Search
- Linear Search Visualisation
- Time & Space Complexity
- Pros and Cons of Linear Search
- Linear Search on a Sorted List
- Sorted Linear Search in Python