Linear search program in C

Are you new to programming and struggling to understand how searching works in C? Let’s simplify it! In this guide, we’ll break down the linear search program in C step-by-step, with real-life examples and code even a 10-year-old can grasp. By the end, you’ll know how to find elements in an array like a pro!


What is Linear Search?

Imagine you’ve lost your favorite toy in a messy room. How do you find it? You check each item one by one until you spot it. That’s exactly what linear search does! It’s a basic algorithm that checks every element in a list (or array) sequentially until it finds the target value.

Why Learn Linear Search?

  • Perfect for small datasets.
  • Simple to implement (no complex math!).
  • Works on both sorted and unsorted arrays.

How Does Linear Search Work?

Let’s say we have an array: [5, 3, 8, 2, 9] and want to find the number 8. Here’s how linear search works:

  1. Start from the first element (5).
  2. Compare it with 8 → Not a match.
  3. Move to the next element (3) → Not a match.
  4. Next element is 8 → Match found!

Time Complexity: O(n) – Worst case, it checks all elements.


Step-by-Step Linear Search Program in C

#include <stdio.h>  

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

int main() {  
    int arr[] = {5, 3, 8, 2, 9};  
    int target = 8;  
    int size = sizeof(arr) / sizeof(arr[0]);  

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

    if (result != -1) {  
        printf("Element found at position: %d", result);  
    } else {  
        printf("Element not found!");  
    }  
    return 0;  
}  

Code Explanation

  1. Function Definition: linearSearch() takes three arguments: the array, its size, and the target value.
  2. Loop Through Array: The for loop checks each element against the target.
  3. Return Position: If found, returns the index (position). If not, returns -1.

Real-Life Example

Think of linear search like scrolling through your WhatsApp chats to find a specific message. You start from the top and scroll down until you spot it. It’s not the fastest method for long chats, but it works!


When to Use Linear Search?

  • Your list has fewer than 100 elements.
  • The list is unsorted.
  • You need a quick, no-frills solution.

Pro Tip: For large datasets, use binary search (but only if the array is sorted!).


Common Mistakes to Avoid

  1. Forgetting Array Bounds: Always loop from 0 to size-1.
  2. Ignoring Return Values: Check if the function returns -1 to handle “not found” cases.

FAQs

Q1. What if there are duplicate elements?
A1. Linear search returns the first occurrence of the target. Modify the code to collect all positions if needed.

Q2. Is linear search inefficient?
A2. For small data, it’s fine. For large data, use better algorithms like binary search.


Practice Exercise

Modify the program to count how many times a number appears in the array.
(Hint: Use a counter variable inside the loop!)


Linear search is the ABC of searching algorithms. It’s easy, intuitive, and a great starting point for new programmers. Try tweaking the code above, experiment with different arrays, and soon you’ll master searching in C!

Found this helpful? Share it with a friend struggling with C programming!

Buy Best Coding Laptop with discount rate – https://amzn.to/3X2EgHb

Leave a reply

Recent Comments

No comments to show.
Join Us
  • Facebook38.5K
  • X Network32.1K
  • Behance56.2K
  • Instagram18.9K

Stay Informed With the Latest & Most Important News

I consent to receive newsletter via email. For further information, please review our Privacy Policy

Categories

Advertisement

Loading Next Post...
Follow
Sign In/Sign Up Sidebar Search Trending 0 Cart
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...

Cart
Cart updating

ShopYour cart is currently is empty. You could visit our shop and start shopping.