Linear Search:
The linear search method is mainly applicable for searching elements with an unordered list. In the linear Search method each element of the list is compared with the key in a sequential order. Linear Search is also called as Sequential Search.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,val,a[20],j=0;
clrscr();
printf("\nEnter the Number of Elements:");
scanf("%d",&n);
printf("\nEnter %d of elements:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the Search element:");
scanf("%d",&val);
for(i=0;i<n;i++)
{
if(a[i]==val)
{
printf("\nElement Found");
}
else
{
j=j+1;
}
}
if(i==j)
{
printf("\nElement not found");
}
getch();
}
Output:
Enter the Number of Elements:4
Enter 4 of elements:6
2
1
9
Enter the Search element:10
Element not found
The linear search method is mainly applicable for searching elements with an unordered list. In the linear Search method each element of the list is compared with the key in a sequential order. Linear Search is also called as Sequential Search.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,val,a[20],j=0;
clrscr();
printf("\nEnter the Number of Elements:");
scanf("%d",&n);
printf("\nEnter %d of elements:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the Search element:");
scanf("%d",&val);
for(i=0;i<n;i++)
{
if(a[i]==val)
{
printf("\nElement Found");
}
else
{
j=j+1;
}
}
if(i==j)
{
printf("\nElement not found");
}
getch();
}
Output:
Enter the Number of Elements:4
Enter 4 of elements:6
2
1
9
Enter the Search element:10
Element not found
0 comments:
Post a Comment