Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.
Though it is very easy in python to check for an item's existence in a list by:
item in list
and finding an item's index by:
list.index(item)
but sometimes you might need to do some customization on the searching algorithm. Like- 'finding the last occurrence of the item in the list' etc. This linear search implementation will help you to do that.
- Worst Case - O(n)
- Best Case - O(1)
- Average Case - O(n)