Introduction to Searching Data Structure and Algorithm Tutorial (original) (raw)

**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.

Searching-algorithm

Introduction to Searching - Data Structure and Algorithm Tutorial

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

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.

Applications of Searching:

Searching algorithms have numerous applications across various fields. Here are some common applications:

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:

  1. Linear Search
  2. Binary Search
  3. Ternary Search
  4. Jump Search
  5. Interpolation Search
  6. Fibonacci Search
  7. Exponential 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.

Linear-Search

Linear Search

Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and **key = 30

Start 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.

Linear-Search-Algorithm-1

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

Linear-Search-Algorithm-2

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.

Linear-Search-Algorithm-3

LinearSearch(collection, key):

for each element in collection:

if element is equal to key:

return the index of the element

return "Not found"

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).

Binary-Seach-1

Binary Search Algorithm

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

Binary-Seach-Algorithm-1

Binary-Seach-Algorithm-2

Binary-Seach-Algorithm-3

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"

Ternary Search is a searching algorithm that divides the search space into **three parts instead of two, as in Binary Search. It is very useful in the case of unimodal functions.

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

Ternary-Search

Ternary 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.

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.

Performance Comparison based on Complexity:

**linear search < jump search < binary search

**Interpolation Search is an efficient searching algorithm for **sorted collections of data, such as arrays or lists. It is an improvement over **Binary Search, particularly when the data is uniformly distributed.

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

**Exponential Search is a searching algorithm designed to find a **target value in a sorted collection, such as an array or list. It combines elements of Binary Search and **Linear Search to efficiently locate the target, especially when its position is near the beginning of the collection.

Easy Problems on Searching:

  1. **Count 1’s in a sorted binary array
  2. **Ceiling in a sorted array
  3. **k largest(or smallest) elements in an array
  4. **Kth smallest element in a row-wise and column-wise sorted 2D array
  5. **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:

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

Hard problems on Searching:

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