Linear search in C++ Program Example Code

Linear search or sequential search is one of the searching algorithms in which we have some data in a data structure like array data structure and we have to search a particular element in it which is known as key.
By traversing the whole data structure elements from start to end one by one to find key comparing with each data structure element to the key.

In case of an array, we check that the given key or a number is present in array at any index or not by comparing each element of array

There can be two possible outcomes if we are assuming that data structure like array contains unique values.


  • Linear search successful Key Found means in array at an index we found value which matches to our key
  • Linear search failed to find the Key mean our key does not exist in data


Linear Search in C++ Program Example Code


#include<iostream>
using namespace std;

int main() {

cout<<"Enter The Size Of Array:   ";
int size;
cin>>size;


int array[size], key,i;


// Taking Input In Array

for(int j=0;j<size;j++){
cout<<"Enter "<<j<<" Element: ";
cin>>array[j];
}

//Your Entered Array Is

for(int a=0;a<size;a++){
  cout<<"array[ "<<a<<" ]  =  ";
  cout<<array[a]<<endl;
}

cout<<"Enter Key To Search  in Array";

cin>>key;

 for(i=0;i<size;i++){

  if(key==array[i]){
cout<<"Key Found At Index Number :  "<<i<<endl;
break;
  }
}


if(i != size){

cout<<"KEY FOUND at index :  "<<i;
}
else{
cout<<"KEY NOT FOUND in Array  ";
}
   return 0;
}



Image View Of Code:



Sample Output:
What is Linear Search In C++ Code Example Sample Output


Code Logic Explanation:

  • To Make Logic First We Think That We Have To Traverse Whole Array Form Start To End So we Decide To Use a Loop 
  • First for Loop Taking Input in Array Element By Element Second Displaying Entered Elements
  • Third for Loop Which is Main For Loop Having an if Condition Which Checks Every Array Element with Key
  • If an Element Matches With Key if Condition Becomes True and Loop Terminates With Break Statement
  • Then If Condition Outside Loop Which Will Become True Because Loop Variable 'i' not Equal to Size Of Array 
  • If Element Not Found In Array Than Loop Will Run Complete And If Condition Will Not True Because in This Case Loop Will Run Complete And After Termination Variable 'i' Will be Equal to Size Variable


Dry Running The Code With Sample Input
Let size=5;

Array Elements are
1
2
3
4
5

Key is equal to 4
3rd for loop comparisons

i=0
if(1==4) false

i=1
if(2==4) false

i=2
if(3==4) false

i=3
if(4==4) True
break for loop

Output:
Key Found At Index Number :  3



For students struggling with their c++ assignments

Frequently students all over the world struggle with preparing C++ assignments and they need help online. If you are looking for someone who could process your "do my C++ homework" request, please address experts from AssignmentCore.com
Bookc
Admin

Hopefully the information we provide can be useful.

Related Article

Press ESC to close