Monday, 1 July 2013

Linier Searching technique in Data structure program with flowchart and algorithm

ALGORITHM

VARIABLES:

§  A:    array name.
§  N:   number of elements in array.
§  Item:      value to be searched.
§  Loc: location of item to be searched.


    ALGORITHM:

STEP 1:    Set a [n+1] = item
STEP 2:    Set LOC = 1
STEP 3:    Repeat while a [loc] != item
                 LOC = LOC + 1
STEP 4:    If LOC = N+1 then,
                 Search is successful
                 else
                 LOC = 0 & search is unsuccessful
STEP 5:    Exit


EXPLANATION:

§  To start the linier search, its is first assumed that the ITEM is inserted into array at the last location N+1
     as                   A [N+1] = ITEM

§  Then, the location counter ‘LOC’ is set to 1, to start the search, the value of the variable to be searched would be started from 1.

§  The location counter is them incremented, till the location of value to e searched becomes equal to the location counter.

§  If the location counter becomes equal to (n+1), then, the value to be searched is present at that location and the search is successful, otherwise, the location counter becomes ‘zero’ and the search is unsuccessful
       IMPLEMENTATION:


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,item;
printf("enter n");
scanf("%d",&n);
printf("enter array elements");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("enter search item");
scanf("%d",&item);
for(i=1;i<=n;i++)
{
if(item==a[i])
{
printf("item present");
goto end;
}
}
goto ss;
ss:printf("item not present");
end:getch();
}

No comments:

Post a Comment