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

  1. Start from the first element.
  2. Compare each element with the target value.
  3. If found, return the index.
  4. 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

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes