Linear Search in C – Beginner-Friendly Tutorial with Example

Linear Search in C – Beginner-Friendly Tutorial with Example

Linear Search in C is one of the simplest and most important searching algorithms for beginners. It is often the first searching technique taught in programming because it is easy to understand and implement.

In this tutorial, you will learn Linear Search in C from scratch, even if you have never written a program before.


What is Linear Search in C?

Linear Search is a searching algorithm used to find an element in a list or array by checking each element one by one.

It starts from the first element and continues searching until:

  • The element is found, or
  • The entire list has been checked

Why is it Called Linear Search?

It is called linear because:

  • Elements are checked in a straight line
  • No skipping or dividing happens
  • Each element is compared sequentially

When Should You Use Linear Search?

Linear Search is useful when:

  • The array is unsorted
  • The dataset is small
  • Simplicity is more important than speed

Linear Search Example (Real Life)

Linear Search in C – Beginner-Friendly Tutorial with Example

Think about finding a name in a class attendance list 📋

You start from the first name and check each one until you find the correct person.

That’s exactly how Linear Search works.


How Linear Search Works (Step-by-Step)

Consider the array:

Index:  0   1   2   3   4
Array:  10  25  30  45  60

We want to search for 45.

Step 1:

Compare 10 with 45 → ❌

Step 2:

Compare 25 with 45 → ❌

Step 3:

Compare 30 with 45 → ❌

Step 4:

Compare 45 with 45 → ✅ Found


Linear Search Algorithm (Simple Steps)

  1. Start from the first element
  2. Compare the current element with the key
  3. If matched → stop search
  4. If not matched → move to next element
  5. Repeat until end of array

Linear Search Program in C

#include <stdio.h>

int linearSearch(int arr[], int size, int key) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == key)
            return i;   // Element found
    }
    return -1;  // Element not found
}

int main() {
    int arr[] = {10, 25, 30, 45, 60};
    int size = 5;
    int key = 45;

    int result = linearSearch(arr, size, key);

    if (result != -1)
        printf("Element found at index %d", result);
    else
        printf("Element not found");

    return 0;
}

Explanation of Linear Search Code in C

  • arr[] → array of elements
  • size → number of elements
  • key → element to search
  • Loop checks each element
  • Returns index if found
  • Returns -1 if not found

This approach is very beginner-friendly.


Output

Element found at index 3

(Indexing starts from 0)


Time Complexity of Linear Search

CaseTime Complexity
Best CaseO(1)
Average CaseO(n)
Worst CaseO(n)

👉 O(n) means time increases as the number of elements increases.


Advantages of Linear Search

  • Very easy to understand
  • Works on unsorted arrays
  • No extra memory required
  • Best for small datasets

Disadvantages of Linear Search

  • Slow for large datasets
  • Not efficient compared to Binary Search
  • Repeats unnecessary comparisons

Linear Search vs Binary Search

FeatureLinear SearchBinary Search
Sorted Array Required❌ No✅ Yes
SpeedSlowFast
ImplementationVery EasyModerate
Best forSmall dataLarge data

Frequently Asked Questions (FAQs)

Is Linear Search better than Binary Search?

Linear Search is simpler, but Binary Search is faster for large sorted arrays.

Can Linear Search work on unsorted arrays?

Yes, that is its biggest advantage.

Is Linear Search used in real applications?

Yes, for small datasets and simple checks.


When NOT to Use Linear Search?

❌ When data is very large
❌ When performance is critical
❌ When faster algorithms are available


Conclusion

Linear Search in C is the best starting point for learning searching algorithms. It helps beginners understand how arrays work and builds a strong foundation for learning Binary Search and advanced algorithms later.


What Next?

If you want, I can:

  • ✅ Add FAQ & HowTo SEO Schema
  • 🎨 Create a click-worthy thumbnail
  • 🔗 Add internal links (Linear vs Binary)
  • 📊 Create comparison charts

Just tell me 👍

See Also

Leave a Comment