Linear Search
Linear Search, also known as Sequential Search, is the simplest searching algorithm that checks each element in a list sequentially until the target element is found or the end is reached. Despite its O(n) time complexity, it's the only option for unsorted data and remains practical for small datasets or when simplicity is crucial.
Visualization
Interactive visualization for Linear Search
Linear Search
Array:
✗ Target 22 not found
• Time: O(n)
• Simple sequential search
• Works on unsorted arrays
Interactive visualization with step-by-step execution
Implementation
1function linearSearch(arr: number[], target: number): number {
2 for (let i = 0; i < arr.length; i++) {
3 if (arr[i] === target) return i;
4 }
5 return -1;
6}Deep Dive
Theoretical Foundation
Linear Search works by traversing the array from the beginning and comparing each element with the target value. If a match is found, the algorithm returns the index of that element. If the loop completes without finding the target, it returns -1 to indicate the element is not present. Unlike Binary Search, Linear Search doesn't require the data to be sorted, making it versatile. The algorithm's simplicity makes it the go-to choice for small datasets (< 100 elements) where the overhead of sorting for Binary Search isn't justified. It's also the only option for unsorted or unordered data structures like linked lists where random access isn't available.
Complexity
Time
O(1)
O(n)
O(n)
Space
O(1)
Applications
Industry Use
Searching in unsorted small lists
Finding elements in linked lists
Validating data in forms and inputs
Simple lookups in configuration files
Searching in streams where data arrives sequentially
Finding first occurrence of a pattern
Menu selection in command-line interfaces
Use Cases
Related Algorithms
Binary Search
Binary Search is one of the most efficient searching algorithms with O(log n) time complexity. It works on sorted arrays by repeatedly dividing the search space in half, eliminating half of the remaining elements with each comparison. This divide-and-conquer approach makes it exponentially faster than linear search for large datasets.
Jump Search
Jump Search is an efficient algorithm for sorted arrays that combines the benefits of linear and binary search. Instead of checking every element (linear) or dividing the array (binary), it jumps ahead by fixed steps of √n and then performs linear search within the identified block. This approach achieves O(√n) time complexity, making it faster than linear search while being simpler than binary search for certain applications.
Interpolation Search
Interpolation Search is an improved variant of binary search specifically optimized for uniformly distributed sorted arrays. Instead of always checking the middle element, it estimates the target's position based on the target value relative to the range of values, similar to how humans search a phone book. Achieves O(log log n) average time for uniformly distributed data, significantly faster than binary search's O(log n).
Exponential Search
Combines exponential growth with binary search. Especially useful for unbounded/infinite arrays.