0% found this document useful (0 votes)
2 views10 pages

Searching Algorithms

The document discusses various searching algorithms used to find records within data structures, categorizing them into sequential and interval searching techniques. It details specific algorithms such as Linear Search, Binary Search, Interpolation Search, and Jump Search, explaining their methodologies and time complexities. The evaluation of these algorithms is based on their performance in best-case and worst-case scenarios, emphasizing the importance of data structure organization for efficient searching.

Uploaded by

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

Searching Algorithms

The document discusses various searching algorithms used to find records within data structures, categorizing them into sequential and interval searching techniques. It details specific algorithms such as Linear Search, Binary Search, Interpolation Search, and Jump Search, explaining their methodologies and time complexities. The evaluation of these algorithms is based on their performance in best-case and worst-case scenarios, emphasizing the importance of data structure organization for efficient searching.

Uploaded by

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

Searching Algorithms

Searching is a process of finding a particular record, which can be a single element or a small
chunk, within a huge amount of data. The data can be in various forms: arrays, linked lists, trees,
heaps, and graphs etc. With the increasing amount of data nowadays, there are multiple
techniques to perform the searching operation.

Searching Algorithms in Data Structures

Various searching techniques can be applied on the data structures to retrieve certain data. A
search operation is said to be successful only if it returns the desired element or data; otherwise,
the searching method is unsuccessful.

There are two categories these searching techniques fall into. They are −

 Sequential Searching
 Interval Searching

Sequential Searching

As the name suggests, the sequential searching operation traverses through each element of the
data sequentially to look for the desired data. The data need not be in a sorted manner for this
type of search.

Example − Linear Search

Fig. 1: Linear Search Operation


Interval Searching

Unlike sequential searching, the interval searching operation requires the data to be in a sorted
manner. This method usually searches the data in intervals; it could be done by either dividing
the data into multiple sub-parts or jumping through the indices to search for an element.

Example − Binary Search, Jump Search etc.

Binary_Search_Operation
Fig. 2: Binary Search Operation

Evaluating Searching Algorithms

Usually, not all searching techniques are suitable for all types of data structures. In some cases, a
sequential search is preferable while in other cases interval searching is preferable. Evaluation of
these searching techniques is done by checking the running time taken by each searching method
on a particular input.

This is where asymptotic notations come into the picture. To learn more about Asymptotic
Notations, please click here.

To explain briefly, there are three different cases of time complexity in which a program can run.
They are −

 Best Case
 Average Case
 Worst Case
We mostly concentrate on the only best-case and worst-case time complexities, as the average
case is difficult to compute. And since the running time is based on the amount of input given to
the program, the worst-case time complexity best describes the performance of any algorithm.

For instance, the best case time complexity of a linear search is O(1) where the desired element
is found in the first iteration; whereas the worst case time complexity is O(n) when the program
traverses through all the elements and still does not find an element. This is labelled as an
unsuccessful search. Therefore, the actual time complexity of a linear search is seen as O(n),
where n is the number of elements present in the input data structure.

Many types of searching methods are used to search for data entries in various
data structures. Some of them include −

 Linear Search
 Binary Search
 Interpolation Search
 Jump Search
 Hash Table
 Exponential Search
 Sublist Search
 Fibonacci Search
 Ubiquitous Binary Search

Linear Search

Linear search is a type of sequential searching algorithm. In this method, every element within
the input array is traversed and compared with the key element to be found. If a match is found
in the array the search is said to be successful; if there is no match found the search is said to be
unsuccessful and gives the worst-case time complexity.
For instance, in the given diagram, we are searching for an element 33. Therefore, the linear
search method searches for it sequentially from the very first element until it find a match. This
returns a successful search.

Linear search checks each element one by one.

 Best case: The target is found in the first position → Time complexity: O(1)
 Worst case: The target is not found or is last in the list → Time complexity: O(n)

Linear search traverses through every element sequentially therefore, the best case is when the
element is found in the very first iteration. The best-case time complexity would be O(1).
However, the worst case of the linear search method would be an unsuccessful search that does
not find the key value in the array, it performs n iterations. Therefore, the worst-case time
complexity of the linear search algorithm would be O(n).

Binary search

Binary search is a fast search algorithm with run-time complexity of (log n). This search
algorithm works on the principle of divide and conquer, since it divides the array into half before
searching. For this algorithm to work properly, the data collection should be in the sorted form.

Binary search looks for a particular key value by comparing the middle most item of the
collection. If a match occurs, then the index of item is returned. But if the middle item has a
value greater than the key value, the right sub-array of the middle item is searched. Otherwise,
the left sub-array is searched. This process continues recursively until the size of a subarray
reduces to zero.
Step 1 − Select the middle item in the array and compare it with the key value to be searched. If
it is matched, return the position of the median.

Step 2 − If it does not match the key value, check if the key value is either greater than or less
than the median value.

Step 3 − If the key is greater, perform the search in the right sub-array; but if the key is lower
than the median value, perform the search in the left sub-array.

Step 4 − Repeat Steps 1, 2 and 3 iteratively, until the size of sub-array becomes 1.

Step 5 − If the key value does not exist in the array, then the algorithm returns an unsuccessful
search.

The time complexity of the binary search algorithm is O(log n)

The following is our sorted array and let us assume that we need to search the location of value
31 using binary search.

4 is the mid of the array.

Now we compare the value stored at location 4, with the value being searched,
i.e. 31. We find that the value at location 4 is 27, which is not a match. As the
value is greater than 27 and we have a sorted array, so we also know that the
target value must be in the upper portion of the array

Our new mid is 7 now. We compare the value stored at location 7 with our
target value 31.
The value stored at location 7 is not a match; rather it is less than what we are looking for. So,
the value must be in the lower part from this location. Hence, we calculate the mid again. This
time it is 5. We compare the value stored at location 5 with our target value. We find that it is a
match.

Interpolation search algorithm

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.

Time Complexity

Case Complexity
Best Case O(1)
Average O(log log n)
Worst Case O(n)

✅ Better than Binary Search when data is uniformly distributed.

The middle element is given by the formula:


If the middle item is greater than the item, then the probe position is again calculated in the sub-
array to the right of the middle item. Otherwise, the item is searched in the sub-array to the left
of the middle item. This process continues on the sub-array as well until the size of subarray
reduces to zero.

Algorithm for searching

1. Start searching data from middle of the list.

2. If it is a match, return the index of the item, and exit.

3. If it is not a match, probe position.

4. Divide the list using probing formula and find the new middle.

5. If data is greater than middle, search in higher sub-list.

6. If data is smaller than middle, search in lower sub-list.

7. Repeat until match.

-Runtime complexity of interpolation search algorithm is (log (log n)).

To understand the step-by-step process involved in the interpolation search, let us look at an
example and work around it.
Comparing the key element given, that is 19, to the element present in the mid index, it is found
that both the elements match.

Therefore, the element is found at index 2.


JUMP SEARCH ALGORITHM

Jump Search algorithm is a slightly modified version of the linear search algorithm. The main
idea behind this algorithm is to reduce the time complexity by comparing lesser elements than
the linear search algorithm. The input array is hence sorted and divided into blocks to perform
searching while jumping through these blocks.

For example, let us look at the given example below; the sorted input array is searched in the
blocks of 3 elements each. The desired key is found only after 2 comparisons rather than the 6
comparisons of the linear search.

The time complexity of the jump search technique is O(n) and space complexity is O(1).

So the first index 0 + 3 blocks= 4th block.

Example

Step 1

Initialize i = 0, and size of the input array n = 12

Suppose, block size is represented as m=3

Compare A[0] with the key element and check whether it matches,

A[0] = 0 66
A[3] = 14 66

Since it is not a match, i is again incremented by 3.

A[6] = 48 66

i is incremented by 3 again. A[9] is compared with the key element.

A[9] = 88 66

However, 88 is greater than 66, therefore linear search is applied on the current block. After
applying linear search, the pointer increments from 6th index to 7th. Therefore, A[7] is compared
with the key element.

You might also like