Linear search using cpp.
In this tutorial we learn how to search element in linear searching technology.Linear search is a technique searching element in linear order.This technique checks each element of the list until a match is found.
Complexity:
worst complexity: O(n)
Average complexity: O(n)
Space complexity: O(1)
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i,num,n,c=0,pos;
clrscr();
cout<<"\nEnter the size of array:";
cin>>n;
cout<<"\nEnter the element:";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"\nEnter searchable number:";
cin>>num;
for(i=0;i<n;i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
#include<conio.h>
void main()
{
int arr[10],i,num,n,c=0,pos;
clrscr();
cout<<"\nEnter the size of array:";
cin>>n;
cout<<"\nEnter the element:";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"\nEnter searchable number:";
cin>>num;
for(i=0;i<n;i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"\nNumber is not found;";
}
else
{
cout<<num<<"\nFound at position"<<pos;
}
getch();
}
OUTPUT:
No comments:
Post a Comment