Sequential Search
And Complexity Of
Sequential Search ”
By
Worst case
The worst case is that in which the element is found
only at the end i.e. in the last comparison.
Average case
The average case is that in which the element is
found in comparisons more than best case but less
than worst case.
Types of searching
algorithms
There are two types of searching
algorithm which are listed below.
Sequential search
Binary search
Sequential Search
It is also called linear search.
It is the simplest way for finding an element
in a list.
It searches the element sequentially in a list,
no matter whether list is sorted or unsorted.
In case of sorted list in ascending order, the
search is started from 1st element and
continued until the desired element is found
or the element whose value is greater than
the value being searched.
In case of sorted list in descending order,
the search is started from 1st element and
continued until the desired element is found
or the element whose value is smaller than
the value being searched.
If the list is unsorted searching started from
1st location and continued until the element is
found or the end of the list is reached.
SOME DRAWBACKS
It is a very slow process.
It is used only for small amount
of data.
It is a very time consuming
method.
Algorithm
sequential search
This algorithm is used for linear
searching.
ITEM is to be searched in a linear
array DATA having N elements.
LOC is variable which store the location
in case of successful search and in case
of unsuccessful search LOC will contain
0.
LINEAR SEARCH ALGORITHM
Step 1: [Insert ITEM at the end of DATA]
SetDATA[N+1]:=ITEM
Step 2:[Initialize counter] Set LOC:=1
Step 3:[Search for ITEM]
Repeat while DATA[LOC] !=ITEM
Set LOC:=LOC+1.
[End of loop]
Step 4:[Successful?] if LOC=N+1,then Set
LOC:=0
Step 5:Exit [Finish].
Complexity Of Linear search
algorithm
Linear search provide complexity for finding an
element in an array because linear search is a
step-by-step process, in which specific element is
compared with each element of array.
In linear search complexity is due to two cases.
It is possible that required element occurs at the
end of the array. So linear search consumes more
time.
It is also possible that required element is not
present in the given array, this is the worst case.
In this case the algorithm requires f(n)=n+1
comparisons.
If the element is at first position in array then
only one comparison will be needed.
Suppose pk is the probability that ITEM appears in
DATA[K] and suppose q is the probability that ITEM
does not appear in DATA.
(Then p1+p2+………+pn+q=1.)
Since the algorithm uses k comparisons when ITEM
appears in DATA[K], the average number of
comparisons is given by
f(n)=1xp1+2xp2+………+nxpn+(n+1)xq.
In particular, suppose q is very small and ITEM
appears with equal probability in each element of
DATA. Then q is approximately equal to 0 and each pi
=1/n.
Accordingly,
f(n)=1x1/n+2x1/n+……..+nx1/n+(n+1)x0=(1+2+….
+n)x1/n
=n(n+1)/2x1/n=(n+1)/2
That is, in this special case, the average number of
comparisons required to find the location of ITEM is
approximately equal to half the number of elements in
the array.
Message from presenter:
THANK YOU!