Linear Search

Explanation

Linear search is a simple algorithm that sequentially checks each element in a list until a match is found or the entire list has been searched. It works by iterating through the list, comparing each element to the target value. If a match is found, the index of the element is returned. If the target value is not in the list, the algorithm typically returns -1 or null to indicate that the value was not found.

Example Use Case
Suppose you have a list of numbers: [5, 2, 9, 1, 5, 6]. You want to find the number 9 using linear search. The algorithm will start at the beginning of the list (5), compare it to 9 (no match), then move to the next element (2), compare it to 9 (no match), and so on. When it reaches the third element (9), it finds a match and returns the index 2.
Theoretical Deep Dive

The time complexity of linear search is O(n) in the worst case (when the target value is not in the list or is the last element) and O(1) in the best case (when the target value is the first element). The average time complexity is O(n). Linear search is simple to implement but is not efficient for large lists. For larger datasets, more efficient searching algorithms like binary search (which requires a sorted list) are preferred.