Program 1
// Program for Linear Search
#include<iostream>
#define clrscr() system("cls")
using namespace std;
class Search
{
public:
int linearSearch(int ar[],int n,int s);
int pos;
};
int Search::linearSearch(int ar[],int n,int s)
{
int i;
for(i=0;i<n;i++)
{
if(ar[i]==s)
{
pos=i;
return 1;
}
}
return 0;
}
int main()
{
int a[500],n,s;
xyz:cout<<"\n Enter the limit\n";
cin>>n;
if(n<0 || n>500)
{
cout<<"\n Invalid limit enter again";
goto xyz;
}
else
{
int i;
cout<<"\n Enter element in array\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nEnter an element for search";
cin>>s;
Search S1;
if(S1.linearSearch(a,n,s))
{
cout<<"\n Searching success";
cout<<"\n Element found at position: "<<S1.pos+1;
}
else
cout<<"\n Searching not success";
if(S1.pos==0)
cout<<"\nBest Case";
else
if(S1.pos>=0 && S1.pos<n)
cout<<"\nAverage Case";
else
cout<<"\nWorst Case";
}
return 0;
}