In computer science, a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.
LINEAR SEARCH
Aim:
To write a C++ program to implement Linear Search.Algorithm:
Step 1: Input the list of n elements in an array.Step 2: Input the element to be searched.
Step 3: If the array element is equal to the search element then the element is found.
Step 4: If the search element is not matched the number is not found.
Step 5: Print the element and position in the array.
LINEAR SEARCH PROGRAM
#include<iostream>
using namespace std;
int main()
{
int arr[10],i,num,n,c=0,pos;
cout<<"Enter the array size:";
cin>>n;
cout<<"Enter the array element:";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be search:";
cin>>num;
for(i=0;i<n;i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found.....!!!!";
}
else
{
cout<<num<<"Found at position"<<pos;
}
return 0;
}
OUTPUT:
BY
REGU RAM SV
Bro give some more examples for linear search.
ردحذفإرسال تعليق