Linear Search Algorithm
π Data Structure and Algorithm
π 72 views
π
Nov 05, 2025
β± Estimated reading time: 1 min
Linear Search Algorithm
Linear Search is the simplest searching algorithm that checks each element of the list sequentially until the desired element is found or the list ends.
Algorithm Steps
- Start from the first element.
- Compare each element with the target value.
- If found, return the index.
- If not found, return -1.
Example (C++)
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key)
return i;
}
return -1;
}
Time Complexity
- Best Case: O(1)
- Worst Case: O(n)
Advantages
- Simple to implement.
- Works on both sorted and unsorted data.
Disadvantages
- Inefficient for large datasets.
Conclusion
Linear search is suitable for small or unsorted datasets but is inefficient when data size increases.
π Some advanced sections are available for Registered Members
Register Now
Register Now
Share this Post
β Back to Tutorials