A Beginner’s Guide to Linear Search in C++

Introduction

When it comes to searching for a specific element in an array, algorithms play a crucial role in determining how efficiently the task is accomplished. One such basic yet fundamental algorithm is the Linear Search. In this blog post, we’ll explore the concept of Linear Search and understand the C++ code implementation provided above.

Code

#include
using namespace std;
/********************************************/
int linearSearch(int n[],int s,int size)
    {
        for(int i=0;i<size;i++)
        {
            if(n[i]==s)
            {
            return i;
            }
        }

        return -1;
    }

/*********************************************/
int main()
{
   int n[10];
   int s;
   int result;
   cout<<"Enter values in 'int' array"<<endl;
    for(int j=0;j<10;j++) { cin>>n[j];
    }
   cout<<"Enter number to search in array"<<endl; cin>>s;
   int size=sizeof(n)/sizeof(n[0]);
   result=linearSearch(n,s,size);

   if(result==-1)
   {
       cout<<"Value not found!"<<endl;
    }
    else
    {
    cout<<"value is on index"<<result<<endl;
    }
       return 0;

}

Understanding Linear Search

Linear Search is a simple searching algorithm that sequentially checks each element of an array until a match is found or the entire array has been traversed. It is often referred to as a brute-force search approach due to its straightforward nature. Linear Search is effective for small arrays or unsorted data, but for larger datasets, more advanced algorithms like binary search are preferred.

The Code Breakdown

Let’s delve into the code you’ve provided and break it down step by step.

Importing Libraries

#include
using namespace std;

Here, the necessary library, is imported to facilitate input and output operations. The using namespace std; line lets you use the standard namespace without explicitly specifying it in front of standard elements like cout and cin.

Linear Search Function

int linearSearch(int n[], int s, int size)
{
    for(int i = 0; i < size; i++)
    {
        if(n[i] == s)
        {
            return i;
        }
    }
    return -1;
}

The linear search function accepts three arguments: the array n, the element to search for s, and the size of the array size. It iterates through the array and compares each element with the search element. If a match is found, the index of the element is returned. If the entire array is traversed without finding the element, -1 is returned to indicate the element is not present in the array.

Main Function

int main()
{
    int n[10];
    int s;
    int result;
    cout << "Enter values in 'int' array" << endl;
    for(int j = 0; j < 10; j++) { cin >> n[j];
    }
    cout << "Enter number to search in array" << endl; cin >> s;
    int size = sizeof(n) / sizeof(n[0]);
    result = linearSearch(n, s, size);

    if(result == -1)
    {
        cout << "Value not found!" << endl;
    }
    else
    {
        cout << "Value is at index " << result << endl;
    }
    return 0;
}

In the main function, an array n of size 10 is declared, and the user is prompted to input values for this array. Then, the user is asked to input the number they want to search for (s). The size of the array is calculated using the formula size of (n) / size of (n[0]). The linear search function is called, and the result is stored in the result variable. Depending on the result, a suitable message is displayed to the user.

Conclusion

Linear Search, though not the most efficient algorithm for large datasets, is a great starting point for understanding basic searching techniques. The C++ code provided in this blog post demonstrates the implementation of Linear Search clearly and concisely. As you delve deeper into the world of programming, you’ll discover more advanced searching and sorting algorithms that cater to different scenarios and data types.

Advantages of Linear Search

Simplicity: Linear Search is incredibly simple to understand and implement, making it ideal for beginners.

Universal Applicability: It can be used on unsorted data and works well for small datasets.

Minimal Memory Usage: Linear Search doesn’t require additional data structures, conserving memory.

Disadvantages of Linear Search

Inefficiency with Large Data: For larger datasets, Linear Search can be time-consuming, as it scans every element.

Performance: Linear Search has a linear time complexity of O(n), which can be slow for larger arrays.

FAQs

Is Linear Search only applicable to integers?

No, Linear Search can be used with various data types. The provided code uses integers, but you can adapt it for other types like characters, floating-point numbers, or custom structures.

Can Linear Search work on sorted arrays?

Yes, Linear Search can work on sorted arrays as well. However, it’s more efficient to use binary search for sorted data due to its logarithmic time complexity.

Is Linear Search used in real-world applications?

Yes, Linear Search has its uses. For example, in situations where the dataset is small and not expected to grow significantly, or when simplicity is more important than speed.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top