568 Programming and Data Structures
The searching can be classified into the following types.
1) Linear search (sequential)
2) Binary search
16.3 LINEAR (SEQUENTIAL) SEARCH
The linear search is a usual method of searching data. The linear search is a sequential search. It is
the simple and the easiest searching method. In this method, the searching of an item starts from
the first item and goes up to the last item until the expected item to be found. An element that is to
be searched is checked in the entire data structure in a sequential way from starting to end. Hence,
it is called linear search. Though, it is straight forward, it has serious limitations. It consumes more
time and reduces the retrieval rate of the system. The linear or sequential name implies that the
items are storied in a systematic manner. The linear search can be applied on sorted or unsorted
linear data structure.
The number of iterations for searching depends on the location of an item. If it were located at
first position then the number of iteration required would be 1. Here, least iterations would be
needed hence, this comes under best case.
In case the item to be searched is observed somewhere at the middle then the number of iterations
would be approximately N /l, where N is the total number of iterations. This comes under average
number of iterations.
In the worst case to search an item in a list, N iterations would be required provided the expected
item is at the end of the list.
A simple example is illustrated as follows.
16.1 Write program to search an element from the given array using linear search.
#in clu de<stdio .h>
#include<conio.h>
main()
{
in t j ,num[5] , f ;
c lrs c r () ;
p r in t f ( "Enter the array elements: )/
fo r ( j=0; j<5 ; j++)
sc a n f( "%d", tnum [j]) ;
p r in t f ( "Enter the number from the l i s t fo r search in g:);
sc a n f( "%d", &f) ;
fo r (j « 0 ;j < 5 ;j + + )
i f ( f-anum[j ] )
break;
i f (j<5 )
{
p rin tf("T h e p osition o f element %d i s : % d ",f,+ + j);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.231.194