You are on page 1of 13

# “ Searching, Sequential Search And Complexity Of Sequential Search ”

By Junaid Ali Siddiqui

Searching
 Computer systems are often used to store large amounts of data from which individual records are retrieved according to some searching criteria.  The process of finding the location of a specific data item or record with a given key value or finding the locations of all records which satisfy one or more conditions in a list is called “Searching”.  If the item exists in the given list then search is said to be successful otherwise if the element is not found in the given list then search is said to be unsuccessful.  Searching a telephone number in a telephone directory is an example of searching.

MAIN TYPES OF SEARCHING
Internal Search
 The search in which the whole list resides in the main memory is called internal search.

External search
 The search in which most of the list resides in the secondary memory is called external search.

SOME IMPORTANT POINTS

In Data structures when we use the word searching ,we actually refer to internal searching. The complexity of any searching method is determined from the number of comparisons performed among the elements of the list in order to find the element. The time required for a search operation depends on the complexity of the searching algorithm.

Basically we have to consider three cases when we search for a particular element in the list.
Best case
 The best case is that in which the element is found during the first comparison.

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:
“Try to hide bad qualities of others Allah will hide yours in the day of judgment....”

THANK YOU!