Logic
Linear search starts with the 1st element and goes on searching sequentially element by element till a match is found or until all elements have been searched.
Code
#include<stdio.h>
void linsearch(int[],int);
int main()
{
int a[15],n,i;
printf("\n Enter no of elements ");
scanf("%d",&n);
printf("\n Enter elements ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
linsearch(a,n);
return 0;
}
void linsearch(int a[],int n)
{
int i,key;
printf("\n Enter key to be searched ");
fflush(stdin);
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("\n Element found at position = %d",i + 1);
break;
}
}
if(i==n)
printf("\n Element not found");
}
No comments:
Post a Comment