** Searching **is the fundamental process of locating a specific element or item within a collection of data. This collection of data can take various forms, such as arrays, lists, trees, or other structured representations.

The primary objective of searching is to determine whether the desired element exists within the data, and if so, to identify its precise location or retrieve it. It plays an important role in various computational tasks and real-world applications, including information retrieval, data analysis, decision-making processes, and more.

## Importance of Searching in DSA

Efficient searching algorithms improve program performance.**Efficiency:**Quickly find and retrieve specific data from large datasets.**Data Retrieval:**Enables fast querying of databases.**Database Systems:**Used in a wide range of problem-solving tasks.**Problem Solving:**

## Characteristics of Searching

Understanding the characteristics of searching in data structures and algorithms is crucial for designing efficient algorithms and making informed decisions about which searching technique to employ. Here, we explore key aspects and characteristics associated with searching:

### 1. Target Element:

In searching, there is always a specific target element or item that you want to find within the data collection. This target could be a value, a record, a key, or any other data entity of interest.

### 2. Search Space:

The search space refers to the entire collection of data within which you are looking for the target element. Depending on the data structure used, the search space may vary in size and organization.

### 3. Complexity:

Searching can have different levels of complexity depending on the data structure and the algorithm used. The complexity is often measured in terms of time and space requirements.

### 4. Deterministic vs Non-deterministic:

Some searching algorithms, like binary search, are deterministic, meaning they follow a clear and systematic approach. Others, such as linear search, are non-deterministic, as they may need to examine the entire search space in the worst case.

## Searching Algorithms:

Searching Algorithms are designed to check for an element or retrieve an element from any data structure where it is stored.

Below are some searching algorithms:

- Linear Search
- Binary Search
- Ternary Search
- Jump Search
- Interpolation Search
- Fibonacci Search
- Exponential Search

**1. ****Linear Search****:**

**1.**

**Linear Search**

**:**

Linear Search, also known as Sequential Search, is one of the simplest and most straightforward searching algorithms. It works by sequentially examining each element in a collection of data(array or list) until a match is found or the entire collection has been traversed.

### Algorithm of Linear Search:

- The Algorithm examines each element, one by one, in the collection, treating each element as a potential match for the key you’re searching for.
- If it finds any element that is exactly the same as the key you’re looking for, the search is successful, and it returns the index of key.
- If it goes through all the elements and none of them matches the key, then that means “No match is Found”.

### Illustration of Linear Search:

Consider the array

andarr[] = {10, 50, 30, 70, 80, 20, 90, 40}key = 30Start from the first element (index 0) and compare key with each element (arr[i]). Comparing key with first element arr[0]. Since not equal, the iterator moves to the next element as a potential match.

Comparing key with next element arr[1]. Since not equal, the iterator moves to the next element as a potential match.

Now when comparing arr[2] with key, the value matches. So the Linear Search Algorithm will yield a successful message and return the index of the element when key is found.

### Pseudo Code for Linear Search:

LinearSearch(collection, key):

for each element in collection:

if element is equal to key:

return the index of the element

return “Not found”

### Complexity Analysis of Linear Search:

**Time Complexity:**: In the best case, the key might be present at the first index. So the best case complexity is O(1)**Best Case**In the worst case, the key might be present at the last index i.e., opposite to the end from which the search has started in the list. So the worst-case complexity is O(N) where N is the size of the list.**Worst Case:**O(N)**Average Case:**

O(1) as except for the variable to iterate through the list, no other variable is used.**Auxiliary Space:**

### When to use Linear Search:

- When there is small collection of data.
- When data is unordered.

## 2. Binary Search:

Binary Search is defined as 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).**### Algorithm of Binary Search:

- Divide the search space into two halves by finding the middle index “
”.**mid** - Compare the middle element of the search space with the
.**key** - If the
is found at middle element, the process is terminated.**key** - If the
is not found at middle element, choose which half will be used as the next search space.**key**- If the key is smaller than the middle element, then the
side is used for next search.**left** - If the key is larger than the middle element, then the
side is used for next search.**right**

- If the key is smaller than the middle element, then the
- This process is continued until the key is found or the total search space is exhausted.

### Illustration of Binary Search:

Consider an array

=arr[], and the{2, 5, 8, 12, 16, 23, 38, 56, 72, 91}=target.23

- Calculate the
and compare the mid element with the key. If the key is less thanmidelement, move tomidand if it is greater than theleftthen move search space to themid.right- Key (i.e., 23) is greater than current
element (i.e., 16). The search space moves to themid.right

is less than the current midKey. The search space moves to the56.left

- If the
matches the value of the mid element, the element is found and stop search.key

### Pseudo Code for Binary Search:

Below is the pseudo code for implementing binary search:

binarySearch(collection, key):

left = 0

right = length(collection) – 1

while left <= right:

mid = (left + right) // 2

if collection[mid] == key:

return mid

elif collection[mid] < key:

left = mid + 1

else:

right = mid – 1

return “Not found”

### Complexity Analysis of Binary Search:

**Time Complexity:**O(1) – When the key is found at the middle element.**Best Case:**O(log N) – When the key is not present, and the search space is continuously halved.**Worst Case:**O(log N)**Average Case:**

: O(1)**Auxiliary Space**

### When to use Binary Search:

- When the data collection is monotonic (essential condition) in nature.
- When efficiency is required, specially in case of large datasets.

## 3. Ternary Search:

Ternary Search is a searching algorithm that divides the search space into ** three parts** instead of two, as in

**. It is very useful in the case of unimodal functions.**

**Binary Search**### Algorithm Ternary Search:

- In Ternary Search, start with two midpoints,
and**oneThird**, which divide the collection into three roughly equal parts.**twoThirds** - Compare the elements at
and**oneThird**with the target key you’re searching for.**twoThirds** - Three Possibilities:
- If
contains the key, you’re done and return the index of**oneThird**.**oneThird** - If
contains the key, you’re done and return the index of**twoThirds**.**twoThirds** - If the
is less than the element at**key**, eliminate the rightmost one-third of the collection and focus on the left two-thirds.**oneThird**

- If
- If the key is greater than the element at
, eliminate the leftmost one-third of the collection and focus on the right two-thirds.**twoThirds** - Repeat this process iteratively until either
is found or determine that it’s not present in the collection.**key**

### Example of Ternary Search:

Consider an array

=arr[]and the{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},=target.6

### Complexity Analysis of Ternary Search:

**Time Complexity:**- Best Case: O(1)
- Worst Case: O(log
_{3}N) - Average Case: O(log
_{3}N)

O(1)**Auxiliary Space:**

## 4. Jump Search:

Jump Search is another searching algorithm that can be used on sorted collections (arrays or lists). The idea is to reduce the number of comparisons by jumping ahead by fixed steps or skipping some elements in place of searching all elements.

### Illustration of Jump Search:

Let’s consider the following array: (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610).

The length of the array is 16. The Jump search will find the value of 55 with the following steps assuming that the block size to be jumped is 4.

- Jump from index 0 to index 4;
- Jump from index 4 to index 8;
- Jump from index 8 to index 12;
- Since the element at index 12 is greater than 55, we will jump back a step to come to index 8.
- Perform a linear search from index 8 to get the element 55.

### Time Complexity of Jump Search:

, where “n” is the number of elements in the collection. This makes it more efficient than Linear Search but generally less efficient than Binary Search for large datasets.**Time Complexity: O(√n)****Auxiliary Space:**, as it uses a constant amount of additional space for variables.**O(1)**

### Performance Comparison based on Complexity:

linear search < jump search < binary search

## 5. Interpolation Search

is an efficient searching algorithm forInterpolation Searchcollections of data, such as arrays or lists. It is an improvement oversorted, particularly when the data is uniformly distributed.Binary Search

## 6. Fibonacci Search

is an efficient searching algorithm used for finding aFibonacci Searchvalue in a sorted collection, such as an array or list. It is similar in principle totargetbut uses Fibonacci numbers to determine the positions to be compared.Binary Search

## 7. Exponential Search

is a searching algorithm designed to find aExponential Searchvalue in a sorted collection, such as an array or list. It combines elements oftargetandBinary Searchto efficiently locate the target, especially when its position is near the beginning of the collection.Linear Search

## Easy Problems on Searching:

**Count 1’s in a sorted binary array****Ceiling in a sorted array****k largest(or smallest) elements in an array****Kth smallest element in a row-wise and column-wise sorted 2D array****Given an array of of size n and a number k, find all elements that appear more than n/k times****.**

## Medium problems on Searching:

**Find a peak element****Search an element in a sorted and rotated array****Find the minimum element in a sorted and rotated array****Find the closest pair from two sorted arrays****Allocate Minimum Number of Pages from N books to M students****Assign stalls to K cows to maximize the minimum distance between them**

## Hard problems on Searching:

**Median of two sorted arrays****Median of two sorted arrays of different sizes****Search in an almost sorted array****Find position of an element in a sorted array of infinite numbers****Given a sorted and rotated array, find if there is a pair with a given sum****Longest Increasing Subsequence Size (N log N)**