1 Dec 2019

  • December 01, 2019
  • Amitraj
Linear Search

-> Linear search is the simplest search algorithm and often called sequential search. In this type of searching, we simply traverse the list completely and match each element of the list with the item whose location is to be found. If the match found then location of the item is returned otherwise the algorithm return NULL.



/* C program to implement Linear Search or Sequential searching */

#include<stdio.h>
int main()
{
int a[100],i,n,s;
printf("Enter n values:");
scanf("%d",&n);
printf("Enter %d values:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter a no. to search:");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(a[i]==s)
{
printf("Number found..");
return 0;
}
}
printf("Number not found..");
return 0;
}

OUTPUT:-

1. Enter n values:5
Enter 5 values:69 45 1 6 21
Enter a no. to search:21

Number found..

2. Enter n values:5
Enter 5 values:7 9 14 85 56
Enter a no. to search:87

Number not found..

Translate

Popular Posts