Cse205 L04
Cse205 L04
Key: 55
Steps:
[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: