In computer science, a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.
Concept of Linear Search
Let’s understand the following steps to find the element key = 7 in the given list.
Step – 1: Start the search from the first element and Check key = 7 with each element of list x.
Step – 2: If element is found, return the index position of the key.
Step – 3: If element is not found, return element is not present.
Linear Search Program In Python
Linear Search Complexity
Time complexity of linear search algorithm –
- Base Case – O(1)
- Average Case – O(n)
- Worst Case -O(n)
Linear search algorithm is suitable for smaller list (<100) because it check every element to get the desired number. Suppose there are 10,000 element list and desired element is available at the last position, this will consume much time by comparing with each element of the list.
To get the fast result, we can use the binary search algorithm.