Linear Search is a simple searching algorithm that checks each element in a collection one by one until the target is found or the list ends. It does not require the data to be sorted and is the most straightforward way to search.
- Definition: A search algorithm that sequentially checks each element in the array until the target is found.
- Analogy: Like looking for a name in a phone contact list by checking every entry one by one.
- Core Goal: To find a target value using a direct, step-by-step scan.
- Start from beginning: Begin at the first element of the array.
- Compare each element: Check if current element matches the target.
- Match found: If equal, return its index immediately.
- Move forward: If not, go to the next element.
- End of array: If no match is found, return -1.
- Unsorted data: Works even when data is not sorted.
- Small datasets: Efficient enough for short lists.
- Simple implementation needed: No complex setup required.
- One-time searches: Useful when search frequency is low.
- Basic searching tasks: Used in simple scripts and programs.
- Small-scale systems: Where performance is not critical.
- Embedded systems: Lightweight and easy to implement.
- Fallback method: Used when other search structures are unavailable.
- Assuming it is fast: It has O(n) time complexity, slow for large data.
- Using on large datasets: Inefficient compared to binary search or hashing.
- Skipping edge cases: Forgetting empty array or single-element array handling.
- Not stopping early: Continuing search even after finding the target.
LinearSearch(arr, target)
for i ← 0 to length(arr) - 1 do
if arr[i] == target then
return i
// Target found at index i
return -1
// Target not found in array