Wednesday 26 October 2011

Implementation of Linear Search algorithm

//prg implemented by shadab khan.....
//ALGORITHM....
/*
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

#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();
}
*/

No comments:

Post a Comment