Home | | Object Oriented Programming and Data Structures | Searching - Linear search

Chapter: Object Oriented Programming and Data Structure : Sorting And Searching

Searching - Linear search

A linear search is the most basic of search algorithm you can have. A linear search sequentially moves through your collection (or data structure) looking for a matching value.

Searching

 

Computer systems are often used to store large amounts of data from which individual records must be retrieved according to some search criterion. Thus the efficient storage of data to facilitate fast searching is an important issue. In this section, we shall investigate the performance of some searching algorithms and the data structures which they use.

 

Linear search

 

A linear search is the most basic of search algorithm you can have. A linear search sequentially moves through your collection (or data structure) looking for a matching value.

 

1. Implementation

 

#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:

 


 


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

 

 

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


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming and Data Structure : Sorting And Searching : Searching - Linear search |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.