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)

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)
- Start from the first element
- Compare the current element with the key
- If matched → stop search
- If not matched → move to next element
- 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 elementssize→ number of elementskey→ element to search- Loop checks each element
- Returns index if found
- Returns
-1if not found
This approach is very beginner-friendly.
Output
Element found at index 3
(Indexing starts from 0)
Time Complexity of Linear Search
| Case | Time Complexity |
|---|---|
| Best Case | O(1) |
| Average Case | O(n) |
| Worst Case | O(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
| Feature | Linear Search | Binary Search |
|---|---|---|
| Sorted Array Required | ❌ No | ✅ Yes |
| Speed | Slow | Fast |
| Implementation | Very Easy | Moderate |
| Best for | Small data | Large 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
Linear Search: Practical Considerations
Linear search is not a failed version of binary search. It is a useful baseline when the data is small, unsorted, changing frequently, or stored in a structure where sequential traversal is natural. The key is to understand the cost of checking elements one at a time.
Best, Worst, and Average Cases
| Case | Comparisons | Explanation |
|---|---|---|
| Best | 1 | Target is the first element |
| Worst | n | Target is last or absent |
| Average | About n/2 | Target is found somewhere in the middle on average |
How to Improve a Linear Search Implementation
- Stop immediately when the target is found.
- Use the correct data type for the element and target.
- Avoid unnecessary work inside the loop.
- Keep the comparison logic separate from input/output code.
- For repeated searches over static sorted data, consider sorting once and using a faster search strategy.
Testing Checklist
- Search for the first element.
- Search for the last element.
- Search for a missing value.
- Test an empty array.
- Test duplicate values and verify which matching index your application expects.
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 👍