Wednesday, 22 May 2013

Linear search

Linear search is one of the searching technique using a simple logic of matching each element of the array with the item to be searched.Linear search can be performed on any type of array.. the array may be sorted or unsorted..it works on both.
This method s not efficient when the number of elements is very large. When processing huge amount of data this method gets very slow because it is very time and resource  consuming as each and every element is traversed thus is not used for processing huge data.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int n,i,item,k=0,j;
clrscr();
printf("Enter the number of elements you wnat to enter");
scanf("%d",&n);
printf("\nEnter the elements of array\n");
for(i=0;i<n;i++)
{
 printf("a[%d] =",i);
 scanf("%d",&a[i]);
}

printf("\nThe elements of array are");
for(i=0;i<n;i++)
{
 printf("\na[%d] = %d",i,a[i]);
}

printf("\nEnter the elements of array to be searched");
scanf("%d",&item);
for(k=0;k<n;k++)
{
if(a[k]==item)
{
j=1;
break;
}
}
if(j==1)
printf("success");
else
printf("no success");
getch();
}

No comments:

Post a Comment

full details in json
Proudly Powered by Blogger.
back to top