Linear Search is a simple searching algorithm that checks each element of a collection one by one until the target element is found or the entire collection is traversed.
- Works on both sorted and unsorted collections.
- Takes O(n) time in the worst case.
Examples:
Input: arr[] = [1, 2, 3, 4], x = 3
Output: 2Input: arr[] = [10, 8, 30, 4, 5], x = 5
Output: 4
Approach to Implement Linear Search
Linear Search can be implemented in C++ using the following approaches:
Using std::find()
C++ STL provides the std::find() function to perform a linear search on containers such as arrays, vectors, and lists. It returns an iterator to the first matching element. If the element is not found, it returns the container's end() iterator.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = {1, 2, 3, 4, 5, 8, 9, 11};
// Element to be searched
int key = 8;
// Search for the key
auto it = find(v.begin(), v.end(), key);
// Check whether the element is found
if (it != v.end())
cout << key << " Found at Position: "
<< it - v.begin() + 1;
else
cout << key << " NOT found.";
return 0;
}
Output
8 Found at Position: 6
Explanation
- std::find() searches the vector from the beginning and returns an iterator pointing to key if it is found.
- The position is calculated using it - v.begin() + 1; if the iterator reaches v.end(), the element is not present.
Manually Implement Linear Search
Linear Search can also be implemented manually using iteration or recursion. In both approaches, elements are checked sequentially from the beginning until the key is found.
Iterative Implementation
The iterative approach uses a loop to compare each element with the target value.
- Start from the first element.
- Compare the current element with the target.
- Return its index if a match is found.
- Continue until the array ends.
- Return -1 if the target is not found.
#include <iostream>
#include <vector>
using namespace std;
int linearSearch(const vector<int>& v, int key)
{
// Traverse the vector
for (int i = 0; i < v.size(); i++)
{
// Check if the current element matches the key
if (v[i] == key)
return i;
}
// Key was not found
return -1;
}
int main()
{
vector<int> v = {1, 2, 3, 4, 5, 8, 9, 11};
// Value to search
int key = 8;
// Perform linear search
int index = linearSearch(v, key);
// Display the result
if (index != -1)
cout << key << " Found at Position: "
<< index + 1;
else
cout << key << " NOT found.";
return 0;
}
Output
8 Found at Position: 6
Explanation
- linearSearch() returns the 0-based index of the matching element, which is then converted to a 1-based position using index + 1.
- The if-else block displays whether the searched key was found along with its position.
Recursive Implementation
The recursive approach checks the current element and calls the same function for the next index until the key is found or the end of the vector is reached.
- Start from index 0.
- Return -1 when the end of the vector is reached.
- Return the current index if the key matches.
- Otherwise, recursively search from the next index.
#include <iostream>
#include <vector>
using namespace std;
int linearSearch(const vector<int>& v, int key, int i)
{
// Base case: entire vector has been searched
if (i == v.size())
return -1;
// Check the current element
if (v[i] == key)
return i;
// Search the next element recursively
return linearSearch(v, key, i + 1);
}
int main()
{
vector<int> v = {1, 2, 3, 4, 5, 8, 9, 11};
// Value to search
int key = 8;
// Perform recursive linear search
int index = linearSearch(v, key, 0);
// Display the result
if (index != -1)
cout << key << " Found at Position: "
<< index + 1;
else
cout << key << " NOT found.";
return 0;
}
Output
8 Found at Position: 6
Explanation
- The linearSearch() function checks one element at a time and calls itself with the next index until the key is found or the vector is fully traversed.
- The main() function passes the search key and starting index 0, then displays the returned index as a 1-based position.