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(); }
http://
Contributed by:
Rohit kakria
I am software developer, moderator of xpode.com
Resourse address on xpode.com
http://www.xpode.com/Print.aspx?Articleid=22
Click here to go on website
|