Linear Search Algorithm

Data Structure and Algorithm 269 views Nov 05, 2025 1 min read

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
Share this Post
🚀 Want to Test Your Knowledge?

Take quizzes related to this topic and see where you stand!

Start Quiz Now
Back to Tutorials