DSA Searching Algorithms


Definition

Searching algorithms help us locate a specific item in a group of elements, such as finding a contact in your phone or a word in a dictionary. They are essential for tasks where you need to find something fast and efficiently from a collection of data.


Why Searching Algorithms Matter

Whenever you're working with data, there's often a need to find whether a certain value exists and where it is. The efficiency of this process directly affects the performance of your application, especially when the dataset is large.


Types of Searching Algorithms

There are several searching techniques, but here are the most commonly used ones:


1. Linear Search

Concept:

Check each item one by one from the beginning until you find the desired value or reach the end.

Example

arr = [3, 8, 2, 5, 9] 
target = 5  

for i in range(len(arr)):     
      if arr[i] == target:         
          Print("Found at index", i) 

Unique Insight:

Like flipping through pages of a notebook one at a time to find a word—you might find it quickly or only on the last page.

Time Complexity: O(n)

Best For: Small or unsorted datasets.


2. Binary Search

Concept:

Only works on sorted data. Start in the middle. If the desired value is smaller than the center point, narrow your focus to the earlier section; if larger, explore the latter part. Repeat until found or the range becomes empty.

Example

arr = [2, 4, 6, 8, 10, 12] 
target = 8 
low = 0 
high = len(arr) - 1  

while low <= high:     
      mid = (low + high) // 2     
      if arr[mid] == target:         
          print("Found at index", mid)         
          break     
     elif arr[mid] < target:         
          low = mid + 1     
     else:         
          High = mid - 1 

Unique Insight:

Imagine looking for a word in a dictionary—you don't start from page one; you jump to the middle and adjust your search direction.

Time Complexity: O(log n)

Best For: Large, sorted datasets.


3. Jump Search

Concept:

Designed for sorted arrays. Instead of checking every element, you jump ahead by a fixed block size (like √n). Once you overshoot or find a range, perform a linear search in that block.

Unique Insight:

Think of skipping steps while climbing stairs—you jump a few at a time and walk back slightly if you overstep.

Time Complexity: O(√n)


4. Interpolation Search

Concept:

Improves binary search by guessing where the value might be based on its size. It assumes values are uniformly distributed.

Unique Insight:

It’s like opening a phone book and jumping straight to "Ravi" because you know "R" comes near the end—rather than halving the book blindly.

Time Complexity: O(log log n) — Best Case

Works Best: When elements are evenly spaced.


5. Exponential Search

Concept:

Starts with small jumps (1, 2, 4, 8...) until it goes beyond the target, then performs binary search in the detected range.

Unique Insight:

Like turning the volume knob faster until it’s too loud, then slowly dialing it down to the perfect level.

Time Complexity: O(log n)


6. Ternary Search

Concept:

Works like binary search, but divides the list into three parts instead of two.

Unique Insight:

Instead of checking the middle, you split the array into thirds and reduce your search to the likely segment.

Time Complexity: O(log₃ n)


Time Complexity Summary

AlgorithmBest CaseAverage CaseWorst Case
Linear SearchO(1)O(n)O(n)
Binary SearchO(1)O(log n)O(log n)
Jump SearchO(1)O(√n)O(√n)
Interpolation SearchO(1)O(log log n)O(n)
Exponential SearchO(log i)O(log i)O(log i)
Ternary SearchO(1)O(log₃ n)O(log n)

Real-Life Applications of Searching

  • Search bars in websites or apps
  • File systems for locating files or folders
  • Databases to fetch matching records
  • Games for pathfinding or looking up player data

Choosing the Right Search Algorithm

ConditionRecommended Search
Data is small or unsortedLinear Search
Data is sortedBinary Search
Data is sorted and size is hugeJump/Exponential
Values are evenly distributedInterpolation

Simple Example Comparison

Let’s say you have a list of 1,000 numbers.

  • Linear Search might check up to 1,000 steps.
  • Binary Search would only take about 10 steps.
  • Jump Search might jump in blocks of 32 (√1000), then search linearly in a small portion.

Summary

  • Searching algorithms help you find data efficiently.
  • Use linear search when nothing is sorted.
  • Use binary or jump search when data is sorted.
  • Special searches like interpolation work better for evenly distributed values.

Prefer Learning by Watching?

Watch these YouTube tutorials to understand DATA STRUCTURES ALGORITHMS Tutorial visually:

What You'll Learn:
  • 📌 Searching & Sorting Techniques | Introduction | Playlist | DAA ADA Data Structure | Algorithms
  • 📌 7.1 Linear Search Algorithm | Linear Search in C | Data Structures Tutorials
Previous Next