What is Linear Search in C: A Comprehensive Guide

Linear Search in C

A linear search in C is a simple way to look through an extensive data set for a specific value. It checks each item in the data set, from the first to the last, until it finds one that matches. When the item being looked for is found, the search is over. If the algorithm can’t find a matching instance, it needs to give a good result and end. The linear search method is simple and works well in two situations. It is often used to solve the following problems:

  • When items on a list are comparatively low
  • How to find a specific item in a random-access array

In this blog, we will begin by understanding what linear search is. Further, we will discuss the importance of linear search and various ways to implement linear search in C using functions and more.

To understand Linear search, let us first understand what searching is.

What is Searching in C?

Perform a search to find a specific item in a group. As the collection, you can use either an array or a linked list.

  • If the item is in the result set, the operation is considered successful, and the index of that item is returned.
  • The search is said to have failed if the element can’t be found.

People mostly use two main ways to look through lists and find what they need. But how the list is set up will decide whether or not an algorithm is used.

  1. Linear Search
  1. Binary Search

You’ll learn the linear search algorithm and how it works as we go through this blog.

What is Linear Search?

Linear or sequential search is the most basic type of search. This method looks for one element at a time by reviewing the whole list, looking for a possible match. If a match is found, the location of the sought-after element is returned.

But a NULL value is returned if the element can’t be found.

A full explanation of how to run the Linear Search Algorithm is given below.

Here are the steps you have to take to make linear search work:

Step 1:

Find the search element (called the “Target element”) in the Array and read it.

Step 2:

The second step is to see if the item being looked for is the same as index 1 of the Array.

Step 3:

If they work together, the Linear Search process will end, and the message “Target element is found” will appear.

Step 4:

If neither of these elements is a match, move on to the following item in the Array and do the same thing.

Step 5:

The next step is to repeat steps 3 and 4 until the search element is compared to the final element in the Array, the Target.

Step 6:

If the last item in the list doesn’t match, the Linear Search Function will stop and show the message “Element not found.”

So far, you’ve learned about the Linear Search Algorithm’s basic meaning and how it works.

You Must Like: Java Constructors in Action: Real-World Examples and Use Cases

Moving forward, let’s discuss pseudo code for linear search in C.

Pseudo Code for Linear Search in C

Here’s an example of pseudo-code for a linear search algorithm in C:

// Function to perform linear searchint linear_search(int arr[], int n, int x) {    // Loop through each element in the array    for (int i = 0; i < n; i++) {        // If the current element is equal to the search value, return its index        if (arr[i] == x) {            return i;        }    }    // If the search value is not found in the array, return -1    return -1;}
// Example usageint main() {    int arr[] = {10, 20, 30, 40, 50};    int n = sizeof(arr) / sizeof(arr[0]);    int x = 30;    int result = linear_search(arr, n, x);    if (result == -1) {        printf(“Element is not present in array”);    } else {        printf(“Element is present at index %d”, result);    }    return 0;}

Note: This is just an example of pseudo-code, and some adjustments may need to be made depending on your specific use case.

Linear Search in C using Function

Linear search is a straightforward search algorithm that goes through each element of an array or list until the desired value is found or all elements have been checked. We can use a function to make linear search work in the C programming language.

Here is an illustration of how to utilize a function in C to do a linear search:

#include <stdio.h>
// Function to perform linear searchint linearSearch(int arr[], int n, int target) {    for (int i = 0; i < n; i++) {        if (arr[i] == target) {            return i;  // Target found, return the index        }    }    return -1;  // Target not found, return -1}
int main() {    int arr[] = {2, 5, 9, 12, 8, 15, 3};    int n = sizeof(arr) / sizeof(arr[0]);    int target = 8;
    // Call the linear search function    int index = linearSearch(arr, n, target);
    if (index == -1) {        printf(“Target not found.\n”);    } else {        printf(“Target found at index %d.\n”, index);    }
    return 0;}

In this example, the linearSearch Function is given an array of integers called arr, its length, n, and a value to look for.

  • It goes through each array element and checks to see if it matches the target value.
  • If a match is found, it gives back the element’s index.
  • If the loop ends without finding a match, it sends back -1 to show that the target could not be found.

In the primary Function, we set up an array called arr, its size, n, and the value we want to find. Then, we use these arguments to call the linearSearch Function and store the index it gives us in the index variable. Lastly, we check whether the index is -1 to see if the target was found. If it is, we print the right message to the console.

Moving forward, let us discuss the linear search program in C using Array.

Linear Search Program in C using Array

Here is an example of a linear search program in C using an array:

#include <stdio.h>
int main() {    int arr[] = {10, 23, 4, 17, 8, 21}; // initialize an array of integers    int n = sizeof(arr) / sizeof(int); // calculate the length of the array    int target = 17; // the value to search for    int i;
    for (i = 0; i < n; i++) {        if (arr[i] == target) { // compare each element with the target            printf(“Target found at index %d.\n”, i);            break;        }    }
    if (i == n) { // target not found        printf(“Target not found.\n”);    }
    return 0;}

In this example, we first make an integer array called arr and fill it with some values. The size of the operator is then used to figure out how long the array n is.

Then, we set a variable called “target” to the value we want to look for. Next, we use a for loop to go through each item in the Array and an if statement to compare each item to the target value.

  • If a match is found, we break out of the loop and print a message with the index where the target was found.
  • If the loop ends without finding a match, we publish a statement saying the target could not be found.

Lastly, we will discuss applications of Linear search.

Applications of Linear Search

Linear search is a simple algorithm that can be used to find a specific value in a list or Array. It can be used in numerous different ways.

Here are a few of the most common ways linear search is used:

  • Searching for a name or number in a phone book
  • Finding a specific item in a supermarket or other store
  • Checking a lottery ticket to see if a number is there
  • Finding a word or phrase in a book or document
  • Using a database to look for a particular record
  • Checking a list or Array to see if a certain item is there or not

Linear search is useful when the data needs to be sorted or indexed, and only a small amount of data needs to be looked through. It’s simple to set up and can be used with any data structure. But linear search can take a long time with big datasets. Instead, faster algorithms like binary search or a hash table can be used.

Conclusion

When there are few items on the list or only one search needs to be done in an unorganized list, linear search is a good choice because it’s easy to use.

But it’s usually best to pre-process the list so that you can use a faster way to look for more than one thing in the same list.

C is one of the most used languages to date. Learning any new language is like adding a new skill to upskill one’s career. Education Nest is a reliable platform that offers various courses that help upskill. So, why wait? Pledge to acquire a new skill today! 

Press ESC to close