Google Search

Saturday, January 28, 2012

ALGORITHM TO SEARCH AN ELEMENT USING LINEAR SEARCH (PROGRAM AND ALGORITHM)


ALGORITHM TO SEARCH AN ELEMENT USING LINEAR SEARCH 

1.  Set k := 1 & loc : = 0
2.  Repeat step 3 & 4 while loc : = 0 &k < = n
3.  If (item = data[k])
       loc : = k
     Else
       K = k + 1
4.  If loc : = 0 ,then
     Print “no. not found”
    Else
      Print “loc is the location of item”
5.  Exit    
               
 linear search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,item,loc=-1;
clrscr();
printf("\nEnter the number of element:");
scanf("%d",&n);
printf("Enter the number:\n");
for(i=0;i<=n-1;i++)
   {
     scanf("%d",&a[i]);
   }
printf("Enter the no. to be search\n");
scanf("%d",&item);
for(i=0;i<=n-1;i++)
   {
    if(item==a[i])
      {
       loc=i;
       break;
     }
   }
if(loc>=0)

   printf("\n%dis found in position%d",item,loc+1);
   else
   printf("\nItem does not exits");
   getch();
     } 

0 comments:

Post a Comment