The operation by which a certain element is located in the given sample space is defined as searching. The search is done when the target value is found in the sample space. Here target value refers to the value that has to be searched in the sample space. Here, we will discuss the most commonly used searching techniques available. They are linear search and binary search. In this post, we will discuss linear search in detail.

Linear Search

The simplest technique used in searching is the Linear Search. It compares the target value with the sample space in a sequential order. The worst case scenario is when the target value is at the end of the sample space and in linear search, the measure of performance is the number of comparisons. The number of comparisons is given by O(n).

Linear search is shown in 3 scenarios based on the sample space. In the first case, the sample space is an unsorted array, whereas the second and third cases are the sample space arranged in ascending and descending orders respectively.

In an unsorted array, the target value is compared with the sample space array, let us name it ss, then the target value is compared with the elements in the sample space starting from ss upto ss[n], where n denotes the position where the target value is located. The program for linear search in an unsorted array is shown below. The comments in the program gives you better understanding.

```#include<stdio.h>
int main()
{
/* Here we initialize the value of the sample space in an array named ss*/
int ss={13,16,11,19,15,12,17,18,14,24};
int a,tar;

/* The target value is to be taken as input */
printf("Enter the number to be searched\n");
scanf("%d",&tar);

/* The for loop is executed until the target value is found*/
for(a=0;a<=9;a++)
{
if(ss[a]==tar)
break;    /* Control moves out of the for loop if the target value is found*/
}
if(a<=9)
printf("\nThe number is found at position %d",a+1); /* Prints the result */
else
printf("The number is not present in the array");
return 0;
}```

The same process is applied to the sample space in which the elements are arranged in the ascending order. The program is shown below.

```#include<stdio.h>
int main()
{
/* Here we initialize the value of the sample space in an array named ss*/
int ss={1,5,7,10,15,17,19,23,30,34};
int a,tar;

/* The target value is to be taken as input */
printf("Enter the number to be searched\n");
scanf("%d",&tar);

for(a=0;a<=9;a++)
{
/* If any element in the sample space has a value greater than or equal to the target or
when the target value has exceeded the largest no., it enters the if loop to print the result*/
if(ss[a]>=tar||tar>ss)
{
if(tar==ss[a])
printf("The number has been found at position %d",a+1);
else
printf("The number is not present in the given array");
break;
}
}
return 0;
}```

The same process is applied to the sample space in which the elements are arranged in the descending order. The program is shown below.

```#include<stdio.h>
int main()
{

/* Here we initialize the value of the sample space in an array named ss*/
int ss={34,30,23,19,17,15,10,7,5,1};
int a,tar;

/* The target value is to be taken as input */
printf("Enter the number to be searched\n");
scanf("%d",&tar);
for(a=0;a<=9;a++)
{

/* If any element in the sample space has a value lesser than or equal to the target or
when the target value is less than the smallest no., it enters the if loop to print the result*/
if(ss[a]<=tar||tar>ss||tar<ss)
{
if(tar==ss[a])
printf("The number has been found at position %d",a+1);
else
printf("The number is not present in the given array");
break;
}
}
return 0;
}```
Share.