linear search in c
PROGRAM:
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0;
printf(“Enter the size of an array: “);
scanf(“%d”,&n);
printf(“Enter the elements of the array: “);
for(i=0;i<=n-1;i++){
scanf(“%d”,&a[i]);
}
printf(“Enter the number to be search: “);
scanf(“%d”,&m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf(“The number is not in the list”);
else
printf(“The number is found”);
return 0;
}
OUTPUT:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found
EXPLANATION:
Linear search, also known as sequential search, is a process that checks every element in the list sequentially until the desired element is found. The computational complexity for linear search is O(n), making it generally much less efficient than binary search (O(log n)). But when list items can be arranged in order from greatest to least and the probabilities appear as geometric distribution (f (x)=(1-p) x-1p, x=1,2), then linear search can have the potential to be notably faster than binary search.
related links….