You are on page 1of 59

SEARCHING

Searching Algorithms are designed to check for an


element or retrieve an element from any data structure
where it is stored.
[I] LINEAR SEARCH ALGORITHM

NGUYEN MINH DAO - STC -UTE 2/22/2023 2


▪ Step 1: First, read the search element (Target element) in the array.
▪ Step 2: In the second step compare the search element with the first element in the
array.
▪ Step 3: If both are matched, display “Target element is found” and terminate the
Linear Search function.
▪ Step 4: If both are not matched, compare the search element with the next element
in the array.
▪ Step 5: In this step, repeat steps 3 and 4 until the search (Target) element is
compared with the last element of the array.
▪ Step 6 – If the last element in the list does not match, the Linear Search Function
will be terminated, and the message “Element is not found” will be displayed.

NGUYEN MINH DAO - STC -UTE 2/22/2023 3


▪ Example 1:
▪ Input: arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170},
x = 110;
▪ Output: 6
▪ Explanation: Element x is present at index 6.
▪ Example 2:
▪ Input: arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170},
x = 175;
▪ Output: -1
▪ Explanation: Element x is not present in arr[].

NGUYEN MINH DAO - STC -UTE 2/22/2023 4


NGUYEN MINH DAO - STC -UTE 2/22/2023 5
NGUYEN MINH DAO - STC -UTE 2/22/2023 6
Time complexity: O(N).
Auxiliary Space: O(1).

NGUYEN MINH DAO - STC -UTE 2/22/2023 7


Time Complexity: O(N)
Auxiliary Space: O(N), for using recursive stack
space.

NGUYEN MINH DAO - STC -UTE 2/22/2023 8


NGUYEN MINH DAO - STC -UTE 2/22/2023 9
▪ Advantages of Linear Search
▪ Linear search is simple to implement and easy to understand.
▪ Linear search can be used irrespective of whether the array is sorted or
not. It can be used on arrays of any data type.
▪ Does not require any additional memory.
▪ It is a well suited algorithm for small datasets.
▪ Drawbacks of Linear Search
▪ Linear search has a time complexity of O(n), which in turn makes it slow
for large datasets.
▪ Not suitable for large array.
▪ Linear search can be less efficient than other algorithms, such as hash
tables.
NGUYEN MINH DAO - STC -UTE 2/22/2023 10
▪ When we are dealing with a small dataset.
▪ When you need to find an exact value.
▪ When you are searching a dataset stored in contiguous
memory.
▪ When you want to implement a simple algorithm.

NGUYEN MINH DAO - STC -UTE 2/22/2023 11


▪ Linear search is a simple and flexible algorithm for finding
whether an element is present within an array.
▪ It sequentially examines each element of the array.
▪ The time complexity of linear search is O(n).
▪ It is used for searching databases, lists, and arrays.

NGUYEN MINH DAO - STC -UTE 2/22/2023 12


NGUYEN MINH DAO - STC -UTE 2/22/2023 13
[2] SENTINEL LINEAR SEARCH
▪ Sentinel linear search is a variation of the standard linear search
algorithm used to find a target value in an array or list.
▪ The basic idea behind this algorithm is to add a sentinel value at the
end of the array which is equal to the target value we are looking for.
This helps to avoid checking the array boundary condition during
each iteration of the loop, as the sentinel value acts as a stopper for
the loop.
▪ Although in worst-case time complexity both algorithms are O(n).
Only the number of comparisons are less in sentinel linear search
than linear search.

NGUYEN MINH DAO - STC -UTE 2/22/2023 14


▪ The basic idea of Sentinel Linear Search is to add an extra element at
the end of the array (i.e., the sentinel value) that matches the search
key. By doing so, we can avoid the conditional check for the end of
the array in the loop and terminate the search early, as soon as we
find the sentinel element.
▪ This eliminates the need for a separate check for the end of the
array, resulting in a slight improvement in the average case
performance of the algorithm..

NGUYEN MINH DAO - STC -UTE 2/22/2023 15


SENTINEL LINEAR SEARCH
▪ Here are the steps for Sentinel Linear Search algorithm:
1. Initialize the search index variable i to 0.
2. Set the last element of the array to the search key.
3. While the search key is not equal to the current element of the
array (i.e., arr[i]), increment the search index i.
4. If i is less than the size of the array or arr[i] is equal to the search
key, return the value of i (i.e., the index of the search key in the
array).
5. Otherwise, the search key is not present in the array, so return -1
(or any other appropriate value to indicate that the key is not
found).

NGUYEN MINH DAO - STC -UTE 2/22/2023 16


Time Complexity: O(N)
Auxiliary Space: O(1)

NGUYEN MINH DAO - STC -UTE 2/22/2023 17


Time Complexity: O(N)
Auxiliary Space: O(1)

NGUYEN MINH DAO - STC -UTE 2/22/2023 18


▪ The number of comparisons is reduced in this search as
compared to a traditional linear search. When a linear
search is performed on an array of size N then in the worst
case a total of N comparisons are made when the element
to be searched is compared to all the elements of the array
and (N + 1) comparisons are made for the index of the
element to be compared so that the index is not out of
bounds of the array which can be reduced in a Sentinel
Linear Search.
▪ So total comparisons in the worst case can be 2*N + 1.

NGUYEN MINH DAO - STC -UTE 2/22/2023 19


▪ But in this search, the last element of the array is replaced
with the element to be searched and then the linear search
is performed on the array without checking whether the
current index is inside the index range of the array or not
because the element to be searched will definitely be
found inside the array even if it was not present in the
original array.
▪ So, the index to be checked will never be out of the bounds
of the array. The number of comparisons in the worst case
there will be (N + 2).

NGUYEN MINH DAO - STC -UTE 2/22/2023 20


[III] BINARY SEARCH

NGUYEN MINH DAO - STC -UTE 2/22/2023 21


▪ Problem: Given a sorted array arr[] of n elements,
write a function to search a given element x in arr[]
and return the index of x in the array. Consider
array is 0 base index.

NGUYEN MINH DAO - STC -UTE 2/22/2023 22


▪ Linear Search Approach: A simple approach is to do a
linear search. The time complexity of the Linear search is
O(n). Another approach to perform the same task is using
Binary Search.
▪ Binary Search is a searching algorithm used in a sorted
array by repeatedly dividing the search interval in half.
The idea of binary search is to use the information that the
array is sorted and reduce the time complexity to O(Log
n).

NGUYEN MINH DAO - STC -UTE 2/22/2023 23


▪ Examples:
▪ Input: arr[] = {10, 20, 30, 50, 60, 80, 110, 130, 140, 170}, x = 110
▪ Output: 6
▪ Explanation: Element x is present at index 6.

▪ Input: arr[] = {10, 20, 30, 40, 60, 110, 120, 130, 170}, x = 175
▪ Output: -1
▪ Explanation: Element x is not present in arr[].

NGUYEN MINH DAO - STC -UTE 2/22/2023 24


NGUYEN MINH DAO - STC -UTE 2/22/2023 25
▪ The basic steps to perform Binary Search are:
1. Sort the array in ascending order.
2. Set the low index to the first element of the array and the high index to the last
element.
3. Set the middle index to the average of the low and high indices.
4. If the element at the middle index is the target element, return the middle index.
5. If the target element is less than the element at the middle index, set the high index
to the middle index – 1.
6. If the target element is greater than the element at the middle index, set the low
index to the middle index + 1.
7. Repeat steps 3-6 until the element is found or it is clear that the element is not
present in the array.

NGUYEN MINH DAO - STC -UTE 2/22/2023 26


▪ Binary Search Algorithm can be implemented in the
following two ways
▪ Iterative Method
▪ Recursive Method

NGUYEN MINH DAO - STC -UTE 2/22/2023 27


ITERATIVE METHOD

NGUYEN MINH DAO - STC -UTE 2/22/2023 28


Time Complexity: O(log n)
Auxiliary Space: O(1)

NGUYEN MINH DAO - STC -UTE 2/22/2023 29


RECURSIVE METHOD
▪ Recursive Method (The recursive method follows the divide and conquer
approach)

NGUYEN MINH DAO - STC -UTE 2/22/2023 30


Time Complexity: O(log n)
Auxiliary Space: O(log n)

NGUYEN MINH DAO - STC -UTE 2/22/2023 31


▪ Binary search is faster than linear search, especially for large arrays. As the size of
the array increases, the time it takes to perform a linear search increases linearly,
while the time it takes to perform a binary search increases logarithmically.
▪ Binary search is more efficient than other searching algorithms that have a similar
time complexity, such as interpolation search or exponential search.
▪ Binary search is relatively simple to implement and easy to understand, making it
a good choice for many applications.
▪ Binary search can be used on both sorted arrays and sorted linked lists, making it
a flexible algorithm.
▪ Binary search is well-suited for searching large datasets that are stored in external
memory, such as on a hard drive or in the cloud.
▪ Binary search can be used as a building block for more complex algorithms, such
as those used in computer graphics and machine learning.
NGUYEN MINH DAO - STC -UTE 2/22/2023 32
▪ We require the array to be sorted. If the array is not sorted, we must first sort it
before performing the search. This adds an additional O(n log n) time complexity
for the sorting step, which can make binary search less efficient for very small
arrays.
▪ Binary search requires that the array being searched be stored in contiguous
memory locations. This can be a problem if the array is too large to fit in memory,
or if the array is stored on external memory such as a hard drive or in the cloud.
▪ Binary search requires that the elements of the array be comparable, meaning that
they must be able to be ordered. This can be a problem if the elements of the
array are not naturally ordered, or if the ordering is not well-defined.
▪ Binary search can be less efficient than other algorithms, such as hash tables, for
searching very large datasets that do not fit in memory.

NGUYEN MINH DAO - STC -UTE 2/22/2023 33


▪ Searching in machine learning: Binary search can be used as a building
block for more complex algorithms used in machine learning, such as
algorithms for training neural networks or finding the optimal
hyperparameters for a model.
▪ Commonly used in Competitive Programming.
▪ Can be used for searching in computer graphics. Binary search can be
used as a building block for more complex algorithms used in computer
graphics, such as algorithms for ray tracing or texture mapping.
▪ Can be used for searching a database. Binary search can be used to
efficiently search a database of records, such as a customer database or a
product catalog.

NGUYEN MINH DAO - STC -UTE 2/22/2023 34


▪ When searching a large dataset as it has a time complexity
of O(log n), which means that it is much faster than linear
search.
▪ When the dataset is sorted.
▪ When data is stored in contiguous memory.
▪ Data does not have a complex structure or relationships.

NGUYEN MINH DAO - STC -UTE 2/22/2023 35


▪ Binary search is an efficient algorithm for finding an
element within a sorted array.
▪ The time complexity of binary search is O(log n).
▪ One of the main drawbacks of binary search is that the
array must be sorted.
▪ Useful algorithm for building more complex algorithms in
computer graphics and machine learning.

NGUYEN MINH DAO - STC -UTE 2/22/2023 36


[IV] TERNARY SEARCH

NGUYEN MINH DAO - STC -UTE 2/22/2023 37


▪ Ternary search is a decrease(by constant) and conquer
algorithm that can be used to find an element in an array. It
is similar to binary search where we divide the array into
two parts but in this algorithm, we divide the given array
into three parts and determine which has the key
(searched element).
▪ We can divide the array into three parts by taking mid1
and mid2 which can be calculated as shown below.
Initially, l and r will be equal to 0 and n-1 respectively,
where n is the length of the array.

NGUYEN MINH DAO - STC -UTE 2/22/2023 38


▪ It is same as the binary search. The only difference is that,
it reduces the time complexity a bit more. Its time
complexity is O(log n base 3) and that of binary search is
O(log n base 2).
▪ mid1 = l + (r-l)/3
▪ mid2 = r – (r-l)/3

NGUYEN MINH DAO - STC -UTE 2/22/2023 39


▪ Steps to perform Ternary Search:
▪ First, we compare the key with the element at mid1. If found
equal, we return mid1.
▪ If not, then we compare the key with the element at mid2. If
found equal, we return mid2.
▪ If not, then we check whether the key is less than the element
at mid1. If yes, then recur to the first part.
▪ If not, then we check whether the key is greater than the
element at mid2. If yes, then recur to the third part.
▪ If not, then we recur to the second (middle) part.
NGUYEN MINH DAO - STC -UTE 2/22/2023 40
Iterative Approach of Ternary Search

NGUYEN MINH DAO - STC -UTE Time Complexity: O(log3n), where n is the size of the array. 2/22/2023 41
Auxiliary Space: O(1)
Recursive Implementation of Ternary Search

NGUYEN MINH DAO - STC -UTE Time Complexity: O(log3n), where n is the size of the array. 2/22/2023 42
Auxiliary Space: O(1)
[V] JUMP SEARCH
▪ Jump search technique also works for ordered lists. It
creates a block and tries to find the element in that block. If
the item is not in the block, it shifts the entire block.
▪ The block size is based on the size of the list. If the size of
the list is n then block size will be √n. After finding a
correct block it finds the item using a linear search
technique. The jump search lies between linear search and
binary search according to its performance.

NGUYEN MINH DAO - STC -UTE 2/22/2023 43


▪ Time Complexity: O(√n)
▪ Space Complexity: O(1)
▪ Example
▪ Input:
▪ A sorted list of data:
▪ 10 13 15 26 28 50 56 88 94 127 159 356 480 567 689 699 780 850 956
995
▪ The search key 356
▪ Output:
▪ Item found at location: 11

NGUYEN MINH DAO - STC -UTE 2/22/2023 44


•Time Complexity: O(√n)
•Space Complexity: O(1)

NGUYEN MINH DAO - STC -UTE 2/22/2023 45


[VI] INTERPOLATION SEARCH
ALGORITHM
▪ For the binary search technique, the lists are divided into
equal parts. For the interpolation searching technique, the
procedure will try to locate the exact position using
interpolation formula.
▪ After finding the estimated location, it can separate the list
using that location. As it tries to find exact location every
time, so the searching time reduces. This technique can
find items easily if the items are uniformly distributed.

NGUYEN MINH DAO - STC -UTE 2/22/2023 46


▪ Time Complexity: O(log2(log2 n)) for average case, and
O(n) for worst case (when items are distributed
exponentially)
▪ Space Complexity: O(1)
▪ Examples
▪ Input − A sorted list of data
▪ 10 13 15 26 28 50 56 88 94 127 159 356 480 567 689 699 780 850 956
995.
▪ The search key 780
▪ Output − Item found at location: 16

NGUYEN MINH DAO - STC -UTE 2/22/2023 47


NGUYEN MINH DAO - STC -UTE 2/22/2023 48
NGUYEN MINH DAO - STC -UTE 2/22/2023 49
▪ Given an array arr[] of size N-1 with integers in the range
of [1, N], the task is to find the missing number from the
first N integers.
▪ Note: There are no duplicates in the list.
▪ Examples:
▪ Input: arr[] = {1, 2, 4, 6, 3, 7, 8}, N = 8
▪ Output: 5
▪ Explanation: The missing number between 1 to 8 is 5

NGUYEN MINH DAO - STC -UTE 2/22/2023 50


NGUYEN MINH DAO - STC -UTE 2/22/2023 51
NGUYEN MINH DAO - STC -UTE 2/22/2023 52
NGUYEN MINH DAO - STC -UTE 2/22/2023 53
NGUYEN MINH DAO - STC -UTE 2/22/2023 54
▪ Given an array of integers arr[], The task is to find the index of first
repeating element in it i.e. the element that occurs more than once
and whose index of the first occurrence is the smallest.
▪ Examples:
▪ Input: arr[] = {10, 5, 3, 4, 3, 5, 6}
▪ Output: 5
▪ Explanation: 5 is the first element that repeats

▪ Input: arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10}


▪ Output: 6
▪ Explanation: 6 is the first element that repeats

NGUYEN MINH DAO - STC -UTE 2/22/2023 55


Complexity Analysis for First Repeating Element
Time Complexity: O(N^2) where N is the size of the array. Here we pick one element and check
it whether it is equal to other elements that are present next to that element.
NGUYEN MINH DAO - STC -UTE 2/22/2023 56
Time Complexity: O(N).
Auxiliary Space: O(N).

NGUYEN MINH DAO - STC -UTE 2/22/2023 57


Time Complexity: O(N).
Auxiliary Space: O(N).

NGUYEN MINH DAO - STC -UTE 2/22/2023 58


NGUYEN MINH DAO - STC -UTE 2/22/2023 59

You might also like