0% found this document useful (0 votes)
92 views12 pages

Searching Algorithms: Welcome To CS221: Programming & Data Structures

The document discusses various searching algorithms including linear search, binary search, interpolation search, and jump search. It provides pseudocode to describe each algorithm and analyzes their time complexities. Linear search has O(n) complexity while binary search and interpolation search have O(log n) complexity on average. Jump search has O(√n) complexity.

Uploaded by

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

Searching Algorithms: Welcome To CS221: Programming & Data Structures

The document discusses various searching algorithms including linear search, binary search, interpolation search, and jump search. It provides pseudocode to describe each algorithm and analyzes their time complexities. Linear search has O(n) complexity while binary search and interpolation search have O(log n) complexity on average. Jump search has O(√n) complexity.

Uploaded by

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

Welcome to CS221: Programming & Data Structures

Searching Algorithms
• Topics covered are: Linear Search, Binary Search, Interpolation Search and Jump Search algorithms
• Note: The slides are adapted from the textbook “Data Structures Using C” by Reema Theraja, Oxford University Press.
• The textbook “Data Structures Using C” is available online
Linear Search/Sequential Search
Algorithm

• Linear search, also called as sequential search, is a very simple


method used for searching an array for a particular value. It
works by comparing the value to be searched with every
element of the array one by one in a sequence until a match is
found.
• Linear search is mostly used to search an unordered list of
elements (array in which data elements are not sorted).

Description of the above algorithm:


• In Steps 1 and 2 of the algorithm, we initialize the value of POS and
I.
• In Step 3, a while loop is executed that would be executed till I is
less than N (total number of elements in the array).
• In Step 4, a check is made to see if a match is found between the
current array element and VAL. If a match is found, then the
position of the array element is printed, else the value of I is
incremented to match the next element with VAL. However, if all
the array elements have been compared with VAL and no match is
found, then it means that VAL is not present in the array.
Complexity of Linear Search Algorithm
Linear search executes in O(n) time where n is the number of
elements in the array. Obviously, the best case of linear search
is when VAL is equal to the first element of the array. In this
case, only one comparison will be made. Likewise, the worst
case will happen when either VAL is not present in the array or
it is equal to the last element of the array. In both the cases, n
comparisons will have to be made. However, the performance
of the linear search algorithm can be improved by using a
sorted array.
Binary Search
• Binary search is a searching algorithm that works efficiently Description of the algorithm:
with a sorted list.
• In Step 1, we initialize the value of variables, BEG, END, and
POS.
Algorithm
• In Step 2, a while loop is executed until BEG is less than or
equal to END.
• In Step 3, the value of MID is calculated.
• In Step 4, we check if the array value at MID is equal to VAL
(item to be searched in the array). If a match is found, then the
value of POS is printed and the algorithm exits. However, if a
match is not found, and if the value of A[MID] is greater than
VAL, the value of END is modified, otherwise if A[MID] is
greater than VAL, then the value of BEG is altered.
• In Step 5, if the value of POS = –1, then VAL is not present in
the array and an appropriate message is printed on the screen
before the algorithm exits.
Working example of Binary Search
Complexity of Binary Search Algorithm
The complexity of the binary search algorithm can be
expressed as f(n), where n is the number of elements in the
array. The complexity of the algorithm is calculated depending
on the number of comparisons that are made. In the binary
search algorithm, we see that with each comparison, the size
of the segment where search has to be made is reduced to
half. Thus, we can say that, in order to locate a particular value
in the array, the total number of comparisons that will be
made is given as
Interpolation Search/Extrapolation Search
• Interpolation search, also known as extrapolation search, is a Interpolation search vs Binary Search
searching technique that finds a specified value in a sorted
array. The concept of interpolation search is similar to how we Interpolation search is similar to the binary search technique.
search for names in a telephone book or for keys by which a However, the important difference between the two techniques is
book’s entries are ordered. For example, when looking for a that binary search always selects the middle value of the
name “Bharat” in a telephone directory, we know that it will be remaining search space. It discards half of the values based on the
near the extreme left, so applying a binary search technique by comparison between the value found at the estimated position
dividing the list in two halves each time is not a good idea. We and the value to be searched. But in interpolation search,
must start scanning the extreme left in the first pass itself. interpolation is used to find an item near the one being searched
for, and then linear search is used to find the exact item
• In each step of interpolation search, the remaining search
space for the value to be found is calculated. The calculation is Visualization of search space:
done based on the values at the bounds of the search space
and the value to be searched. The value found at this
estimated position is then compared with the value being
searched for. If the two values are equal, then the search is
complete.
• However, in case the values are not equal then depending on
the comparison, the remaining search space is reduced to the
part before or after the estimated position.
Interpolation Search/Extrapolation Search
Algorithm
Description of the algorithm:
• The algorithmic steps are similar to Binary Search algorithm
• Please check Step 3, how the value of MID is calculated?

Example:
Given a list of numbers a[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21}.
Search for value 19 using interpolation search technique.
Solution
Low = 0, High = 10, VAL = 19, a[Low] = 1, a[High] = 21
Middle = Low + (High – Low)×((VAL – a[Low]) /(a[High] – a[Low]))
= 0 +(10 – 0) × ((19 – 1) / (21 – 1) ) = 0 + 10 × 0.9 = 9
a[middle] = a[9] = 19 which is equal to value to be searched.
Complexity of Interpolation Search Algorithm
When n elements of a list to be sorted are uniformly
distributed (average case), interpolation search makes about
log(log n) comparisons. However, in the worst case, that is
when the elements increase exponentially, the algorithm can
make up to O(n) comparisons.
Jump Search/Block Search
• In jump search, it is not necessary to scan all the elements in Example:
the list to find the desired value.
Consider an array a[] = {1,2,3,4,5,6,7,8,9}. The length of the array
• We just check an element and if it is less than the desired is 9. If we have to find value 8 then following steps are performed
value, then some of the elements following it are skipped by using the jump search technique.
jumping ahead. After moving a little forward again, the
element is checked.
• If the checked element is greater than the desired value, then
we have a boundary and we are sure that the desired value lies
between the previously checked element and the currently
checked element.
• However, if the checked element is less than the value being
searched for, then we again make a small jump and repeat the
process.
• Once the boundary of the value is determined, a linear search
is done to find the value and its position in the array
Jump Search
Algorithm

How to Choose the Step Length?


For the jump search algorithm to work efficiently, we must define
a fixed size for the step. If the step size is 1, then algorithm is same
as linear search. Now, in order to find an appropriate step size, we
must first try to figure out the relation between the size of the list
(n) and the size of the step (k). Usually, k is calculated as √n.

Complexity of Jump Search Algorithm


Jump search works by jumping through the array with a step size
(optimally chosen to be √n) to find the interval of the value. Once
this interval is identified, the value is searched using the linear
search technique. Therefore, the complexity of the jump search
algorithm can be given as O(√n).
Solve:
1. Given the following array: {0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30}
• Apply Binary Search, Interpolation Search and Jump Search to search for the key 20.
2. Implement Binary Search and Interpolation Search algorithms to search for names in a telephone directory. First of all, think about
suitable data structures to store the names of a telephone directory.

You might also like