searching techniques
TRANSCRIPT
Searching Techniques
Submitted To :- Submitted By :-
Miss Jaspreet Kaur (Assistant Prof.)
Parteek Girdhar (B130020031)
Searching Technique :- It refers to the operation of finding the
location LOC of ITEM in DATA, or printing some message that ITEM does not appear there. The search is said to be successful if ITEM does not appear in DATA and unsuccessful otherwise.
Types of Searching :-Linear Search Binary Search
Linear Search :- It is a technique for finding a particular
element in a list that checks each element in sequence until the desired element is found or the list is finished. The list need not be ordered.
For example:- We have an array of 6 elements. A={1,2,3,4,5,6}
suppose we want to find 5.for this we have to traverse the array by comparing each element of array A one by one.
How Long Does Linear Search Take?Best Case :- In this case, the target value is
in the first element of the array. So it is denoted with O(1).
Worst Case :- In this case, the target value is in the last element of the array. So it is denoted with O(n).
Average Case :- In this case, the target value is somewhere in the array. So this search takes an amount of time proportional to half the length of the array – also proportional to the length of the array – O(n).
Algorithm for Linear Search LINEAR(DATA,N,ITEM,LOC) Here DATA is a linear array with N elements, and ITEM is given
item of information. This algorithm finds the location LOC of ITEM in DATA, or sets LOC:= 0 if the search is unsuccessful.
1. [Insert ITEM at the end of DATA.] Set DATA [N+1]:=ITEM.2. [Initialize counter.] Set LOC:=1.3. [Search for item.] Repeat while DATA[LOC]!=ITEM:
Set LOC:=LOC+1. [End of loop.]4. [Successful?] If LOC =N+1,then: Set LOC:=0.5. Exit.
Binary Search In binary search we first access the middle
element of given array. If the element to be searched is same as
middle element of array then our search is successful.
If not then look weather the element is in left or right half of sub-arrays.
After this apply binary search on sub-arrays.Given array should be ordered.
How a Binary Search Works
Always look at the center value. Each time you get to discard half of the remaining list.
Is this fast ?
For example:- Suppose we have to find the location of a name
in a telephone directory.For this we will apply Binary Search.We will find the middle elements first.If it matches our search element then we will stop here.If not then we will determine which half contains the element and we will access the middle elements of that half.If middle element is not same then repeat the same procedure as we did above for each quarter and so on.
Algorithm:-BINARY(DATA,LB,UB,ITEM,LOC)1. [Initialize = segment variables.] Set
BEG:=LB,END:=UB and MID=INT((BEG+END)\2)2. Repeat step 3 and 4 while BEG<=END and DATA[MID]!
=ITEM.3. If ITEM <DATA[Mid],then: Set END:=MID-1. Else: Set
BEG:=MID+1. [END of If structure.]4. Set MID:=INT((BEG+END)/2) [End of step 2 Loop.]5. If DATA[MID]= ITEM, then: Set LOC:=MID. Else: Set
LOC:=NULL. [End of if structure.]6. Exit.
THANKS ;)