0% found this document useful (0 votes)
6 views23 pages

Cse205 L04

The document discusses various searching techniques in data structures, including Linear Search, Binary Search, and Interpolation Search. It outlines the algorithms for each method, highlighting their operational principles, time complexities, and scenarios for effective use. Key comparisons between the search methods are also provided, emphasizing the conditions under which each technique is most efficient.

Uploaded by

SaHa OnTu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views23 pages

Cse205 L04

The document discusses various searching techniques in data structures, including Linear Search, Binary Search, and Interpolation Search. It outlines the algorithms for each method, highlighting their operational principles, time complexities, and scenarios for effective use. Key comparisons between the search methods are also provided, emphasizing the conditions under which each technique is most efficient.

Uploaded by

SaHa OnTu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Searching

Shifat Jahan Setu


Lecturer
Dept. of CSE
Dhaka International University, Dhaka
Searching
• Searching is an operation or a technique
that helps finds the place of a given element
or value in the list.
• Any search is said to be successful or
unsuccessful depending upon whether the
element that is being searched is found or
not.
Searching techniques
• There are three types of searching in data
structure and analysis
• Linear Search
• Binary Search
• Interpolation Search
Linear Search
• Linear search is a very simple search
algorithm.
• In this type of search, a sequential search is
made over all items one by one.
• Every item is checked and if a match is
found then that particular item is returned.
• Otherwise the search continues till the end
of the data collection.
Linear Search Algorithm
• Linear Search (Array A, Value x)
• Step 1: Set i to 1
• Step 2: if i > n then go to step 7 Step 3: if
A[i] = x then go to step 6
• Step 4: Set i to i + 1
• Step 5: Go to Step 2
• Step 6: Print Element x Found at index i and
go to step 8
• Step 7: Print element not found
• Step 8: Exit
Binary search
• Binary search is a fast search algorithm with
run-time complexity of O(log n).
• This search algorithm works on the principle
of divide and conquer. For this algorithm to
work properly.
• The data collection should be in the sorted
form.
• Binary search looks for a particular item by
comparing the middle most item of the
collection. If a match occurs, then the index
of item is returned.
Binary search
• If the middle item is greater than the item,
then the item is searched in the sub-array to
the right of the middle item.
• Otherwise, the item is searched for in the
sub-array to the left of the middle item.
• The process continues on till the subarray
reduces to zero.
Binary search
• Compare x with the middle element. If x
matches with middle element, we return
the mid index.
• Else If x is greater than the mid element,
then x can only lie in right half subarray
after the mid element. So we recur for
right half.
• Else (x is smaller) recur for the left half.
Binary Search

Algorithm: Binary-Search (L, N, Key)


1. Set Loc := 0, Beg := 1, End :=N and Mid := (Beg + End)/2
2. Repeat steps 3 to 6 while Beg <= End
3. If Key < L[Mid], then Set End := Mid – 1
4. Else if Key>L[Mid] then Set Beg := Mid + 1
5. Else if Key = L[Mid] then Loc:= Mid, print: Loc and exit.
6. Set Mid := (Beg + End)/2 Here
7. If Loc = 0 Write: Item is not in List. L - The list of data items
N – Total no. of items in L
8. Exit. Key – Item to be searched.
Example of Binary Search

List : 6, 10, 20, 30, 55, 75, 190

Key: 55

Steps:

1. 6 10 20 55 75 190 Beg=1, End=7,Mid=4

2. 6 10 20 30 55 190 Beg=5, End=7,Mid=6

3. 6 10 20 30 75 190 Beg=5, End=5,Mid=5

[Item Found]
So, Item 55 is in position 5.
Binary vs. Linear Search
• Input data needs to be sorted in Binary
Search and not in Linear Search
• Linear search does the sequential access
whereas Binary search access data
randomly.
• Time complexity of linear search -O(n),
Binary search has time complexity O(log n).
• Linear search performs equality
comparisons and Binary search performs
ordering comparisons
Interpolation Search
• Interpolation search is an improved variant
of binary search.
• This search algorithm works on the probing
position of the required value.
• For this algorithm to work properly, the
data collection should be in a sorted form
and equally distributed.
Interpolation search usage
• There are cases where the location of target
data may be known in advance.
• For example, in case of a telephone
directory, if we want to search the telephone
number of Morphius Here, linear search and
even binary search will seem slow as we can
directly jump to memory space where the
names start from 'M' are stored.
Interpolation Search Algorithm
• Step 1 - Start searching data from middle of
the list.
• Step 2-If it is a match, return the index of the
item, and exit.
• Step 3-If it is not a match, probe position.
• Step 4- Divide the list using probing formula
and find the new midle.
• Step 5- If data is greater than middle, search
in higher sub-list.
• Step 6-If data is smaller than middle, search
in lower sub-list.
• Step 7-Repeat until match.
Binary Search Performance
• Successful Search
• Best Case - 1 comparison
• Worst Case-log₂N comparisons
• Unsuccessful Search
• Best Case =
• Worst Case-log,N comparisons
• Since the portion of an array to search is cut
into half after every comparison, we compute
how many times the array can be divided into
halves.
• After K comparisons, there will be N/2^k
elements in the list. We solve for K when N/2^k
= 1, deriving K = log₂N.
Comparing N and log,N
Performance
Interpolation search
• A - Array of elements, e - element to be
searched, pos - current position
• Make start = 0 & end = n-1
• Calculate position ( pos ) to start searching
at using formula:

• If A[pos] == e , element found at index pos.


Interpolation search
• Otherwise if e > A[pos] we make start = pos
+1
• Else if e < A[pos] we make end = pos -1
• Do stpes 3, 4, 5, 6 While : start <= end && e
>= A[start] && e =< A[end]
• start <= end - that is untill we have elements
in the sub-array.
• e >= A[start] - element we are looking for is
greater than or equal to the starting element
of sub-array we are looking in.
• e =< A[end] - element we are looking for is
less than or equal to the last element of sub-
Interpolation search example
Thank You

You might also like