searching techniques

11
Searching Techniques Submitted To :- Submitted By :- Miss Jaspreet Kaur (Assistant Prof.) Parteek Girdhar (B130020031)

Upload: er-punit-jain

Post on 21-Feb-2017

94 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Searching techniques

Searching Techniques

Submitted To :- Submitted By :-

Miss Jaspreet Kaur (Assistant Prof.)

Parteek Girdhar (B130020031)

Page 2: Searching techniques

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.

Page 3: Searching techniques

Types of Searching :-Linear Search Binary Search

Page 4: Searching techniques

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.

Page 5: Searching techniques

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).

Page 6: Searching techniques

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.

Page 7: Searching techniques

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.

Page 8: Searching techniques

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 ?

Page 9: Searching techniques

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.

Page 10: Searching techniques

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.

Page 11: Searching techniques

THANKS ;)