Linear Search
#include <conio.h>
#include <iostream.h>
int search(int a[], int k, int si){
int i=0;
while(k!=a[i] && i<si ){ i++; }
if(i<si)
return i;
else
return -1;
}
void main()
{
clrscr();
const int s=5;
int a[s]={3,5,1,4,2};
int key,element;
cout<<"\nEnter the key: ";
cin>>key;
element=search(a,key,s);
if(element!=-1)
cout<<"\nKey found at location ["<<element<<"]" ;
else
cout<<"\nKey was not found";
getch();
}
Click the link below to see more examples
URL: http://ravianeducation.blogspot.com
E-Mail: [email protected]
Farhan: 03008855006