SEQUENTIAL SEARCH / LINEAR SEARCH
Definition Of Linear Search:
A key is a value that you are looking for in an array.The simplest type of searching process is the sequential search.
Note: In the sequential search, each element of the array is compared to the key, in the order it appears in the array, until the first element matching the key is found.
Example:
#include
int main(void)
{
int a[10];
a[0]=20;
a[1]=40;
a[2]=100;
a[3]=80;
a[4]=10;
a[5]=60;
a[6]=50;
a[7]=90;
a[8]=30;
a[9]=70;
cout<< "Enter the number you want to find (from 10 to 100)…";
int key;
cin>> key;
int flag = 0; // set flag to off
for(int i=0; i<10; i++) // start to loop through
the array
{
if (array[i] == key) // if match is found
{
flag = 1; // turn flag on
break ; // break out of for loop
}
}
if (flag) // if flag is TRUE (1)
{
cout<< "Your number is at subscript position"<}
else
{
cout<< "Sorry, I could not find your number in this array."<
return 0;
}
Thankyou,
Rajkumar