# Linear Search in C++

Implementation of Linear Search in C++ Programming Language.

In this article, I will introduce you to the linear search algorithm using the C ++ programming language. A linear search is one of the most basic and useful algorithms, it moves sequentially through a data structure to find the corresponding value this is the reason why it is also known as the sequential search algorithm.

Before implementing the linear search using the C++ programming language, let’s first understand how linear search works.

## How Linear Search Algorithm Works?

The linear search algorithm can be compared to looking for a book in a stack of books, you go through them all until you find the one that you want. Let’s understand it with a more simple example explained below.

Think of the linear search algorithm as a way to find your way through a contact list on your smartphone. A linear search begins at the beginning by reading each name until you find what you are looking for. In n terms of complexity, this is an O(n) search – the time it takes to search the list increases as the contact list increases.

Let’s understand the searching approach of linear search algorithm step by step:

1. It starts the search from the leftmost element of the array and compares one by one the element we are looking for with each element in the array.
2. If it finds a match between the searched element and the elements in the array, it returns the index of that element.
3. If there is no match between the searched element and the elements of the array, then it returns -1.

## Linear Search in C++ Programming Language

Now let’s see how to implement the linear search using the C++ programming language. The code below follows all the steps that I have mentioned above:

```Output:
5
10 20 30 40 50
40
3```

## Conclusion

Linear search is a very simple searching algorithm. It cycles through the elements until the query is found, making it a linear algorithm. Its complexity is O (n), where n is the number of elements to iterate.

So this is how to implement the linear search algorithm by using the C++ programming language. If you want to learn its implementation by using the Python programming language, you can check this article. 