Linear Search
Linear Search is the most basic search algorithm. It works by checking every element of a list sequentially until the target element is found or the end of the list is reached.
1. The Core Mechanism
- Iterate: Start from the first element of the array.
- Compare: Check if the current element is equal to the target.
- Found: If a match is found, return the index.
- Advance: If not, move to the next index and repeat.
- Not Found: If the loop completes without finding a match, return -1.
2. Complexity Analysis
| Scenario | Time Complexity | Space Complexity |
|---|---|---|
| Best Case | O(1) | O(1) |
| Average Case | O(N) | O(1) |
| Worst Case | O(N) | O(1) |
- Best Case: The target is at the very first index.
- Worst Case: The target is at the last index or does not exist in the array.
3. Implementation
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i;
}
}
return -1;
}
4. Characteristics and Use Cases
- Unsorted Data: Linear search is the only option for searching through data that has no inherent order.
- Simplicity: Extremely easy to implement and debug.
- Small Datasets: On very small arrays (e.g., < 10 items), Linear Search can be faster than Binary Search due to its lower constant overhead (no mid-point calculations).
- Data Stream: Useful when data is arriving in a stream and you need to find a value as it arrives.
Interview Pro-Tips: Linear vs. Binary Search
- The Tradeoff: Always mention that while Linear Search is slower for large datasets ($O(N)$ vs $O(\log N)$), it requires No Pre-processing. Binary search requires the array to be sorted ($O(N \log N)$ cost if the array is unsorted).
- Early Exit: If you are searching in a Sorted Array using Linear Search, you can stop as soon as you see a value Greater than the target, which improves the average-case performance slightly.
Technical Summary
Sequential: Checks every slot one-by-one.Brute Force: The simplest search approach.Universal: Works on any data, regardless of order.O(N): Performance decreases linearly with dataset size.