Featured

C++ Program to Perform Linear Search on a Vector

 

C++ Program to Perform Linear Search on a Vector

Introduction

In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found.


C++ Program


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int num, search, found = 0;

    cout << "Enter the size of the vector:" << endl;
    cin >> num;

    vector<int> v(num);

    cout << "Enter " << num << " elements in vector:" << endl;

    for(int i = 0; i < num; i++)
    {
        cin >> v[i];
    }

    cout << "Enter element that you want to search:" << endl;
    cin >> search;

    for(int i = 0; i < num; i++)
    {
        if(v[i] == search)
        {
            cout << search << " is found at the index: " << i << endl;
            found = 1;
            break;
        }
    }

    if(!found)
    {
        cout << search << " is not found." << endl;
    }

    return 0;
}

Sample Output


Enter the size of the vector:
6

Enter 6 elements in vector:
10 20 30 40 50 60

Enter element that you want to search:
40

40 is found at the index: 3

Explanation

Step 1

Read the size of the vector from the user.


int num;
cin >> num;

Step 2

Create a vector of the given size.


vector<int> v(num);

Step 3

Read all elements into the vector.


for(int i = 0; i < num; i++)
{
    cin >> v[i];
}

Step 4

Read the element to search.


cin >> search;

Step 5

Traverse the vector and compare every element with the search element. If both are equal, display the index and stop searching.


if(v[i] == search)
{
    cout << search << " is found at the index: " << i << endl;
    found = 1;
    break;
}
  • If the element is found, the loop stops immediately using break.
  • The variable found is set to 1.

Step 6

If the element is not found after checking all elements, display a message.


if(!found)
{
    cout << search << " is not found." << endl;
}

Dry Run

Input


Vector = {12, 8, 25, 19, 30}
Search Element = 19

Processing

Index Element Comparison Result
0 12 12 == 19 Not Found
1 8 8 == 19 Not Found
2 25 25 == 19 Not Found
3 19 19 == 19 Found

Output


19 is found at the index: 3

Time Complexity

Operation Complexity
Reading Input O(n)
Linear Search O(n)

Overall Time Complexity: O(n)


Space Complexity

  • Vector Storage : O(n)
  • Extra Variables : O(1)

Overall Auxiliary Space Complexity: O(1)


Key Points

  • Uses a vector to store input elements.
  • Checks each element one by one.
  • Stops searching immediately after finding the element.
  • Suitable for both sorted and unsorted vectors.
  • Simple searching algorithm for beginners.

Keywords

C++ Linear Search, Linear Search in Vector, Search Element in Vector C++, C++ Vector Program, C++ Searching Program, Linear Search Algorithm in C++, C++ Interview Programs, Data Structures in C++, C++ Coding Practice, STL Vector Search.


Conclusion

This program demonstrates how to perform a Linear Search on a vector in C++. The algorithm compares the search element with every element until it finds a match or reaches the end of the vector. Although Linear Search has a time complexity of O(n), it is simple to implement and works well for small or unsorted datasets, making it an excellent program for beginners.

Comments

Popular Posts