Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Linear Search

/*******************************************/
/*PROGRAM TO PERFORM LINEAR SEARCH*/
/*******************************************/

searching starts from first element and ends till the element find out or list ends. E.g if we want to find the 4 number from 23553466 list then first 4 will be compare with the 2, not matched then next number 3. This process will continue till the 4 number not find out. in this loop it will make 6 comparison. At 6th position 4 will find out and loop will end

In linear search,

#include<stdio.h>
#include<conio.h>

void main()
{
       int a[10],n,i,item,loc;
       clrscr();
       printf("Enter the size of array: ");
       scanf("%d",&n);
       printf("\nEnter the elements of array:\n");
       for(i=0;i<n;i++)
       {
           scanf("%d",&a[i]);
       }
       printf("\nEnter the item tobe searched: ");
       scanf("%d",&item);

       a[n]=item;
       loc=0;
       while(a[loc]!=item)
       {
              loc=loc+1;
       }
       if(loc==n)
       {
              printf("\nItem %d is not present in the array",item);
       }
      else
       {
              printf("\nItem %d is present at %d location in the array",item,loc+1);
       }
       getch();
}

Share this article   |    Print    |    Article read by 1229 times
Author:
Rohit kakria
I am software developer, moderator of xpode.com
Related Articles:
Related Interview Questions: No related interview question