Searching algorithms are essential tools in computer science used to locate specific items within a collection of data. These algorithms are designed to efficiently navigate through data structures to find the desired information, making them fundamental in various applications such as databases, web search engines, and more.
Table of Content
- What is Searching?
- Searching terminologies
- Importance of Searching in DSA
- Applications of Searching
- Basics of Searching Algorithms
- Searching Algorithms
- Comparisons Between Different Searching Algorithms
- Library Implementations of Searching Algorithms
- Easy Problems on Searching
- Medium Problems on Searching
- Hard Problems on Searching
What is Searching?
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.
Searching terminologies:
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.
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.
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.
Deterministic vs. Non-deterministic:
Some searching algorithms, like binary search, are deterministic, meaning they follow a clear, systematic approach. Others, such as linear search, are non-deterministic, as they may need to examine the entire search space in the worst case.
Importance of Searching in DSA:
- Efficiency: Efficient searching algorithms improve program performance.
- Data Retrieval: Quickly find and retrieve specific data from large datasets.
- Database Systems: Enables fast querying of databases.
- Problem Solving: Used in a wide range of problem-solving tasks.
Applications of Searching:
Searching algorithms have numerous applications across various fields. Here are some common applications:
- Information Retrieval: Search engines like Google, Bing, and Yahoo use sophisticated searching algorithms to retrieve relevant information from vast amounts of data on the web.
- Database Systems: Searching is fundamental in database systems for retrieving specific data records based on user queries, improving efficiency in data retrieval.
- E-commerce: Searching is crucial in e-commerce platforms for users to find products quickly based on their preferences, specifications, or keywords.
- Networking: In networking, searching algorithms are used for routing packets efficiently through networks, finding optimal paths, and managing network resources.
- Artificial Intelligence: Searching algorithms play a vital role in AI applications, such as problem-solving, game playing (e.g., chess), and decision-making processes
- Pattern Recognition: Searching algorithms are used in pattern matching tasks, such as image recognition, speech recognition, and handwriting recognition.
Basics of Searching Algorithms:
- Introduction to Searching – Data Structure and Algorithm Tutorial
- Importance of searching in Data Structure
- What is the purpose of the search algorithm?
Searching Algorithms:
- Linear Search
- Sentinel Linear Search
- Binary Search
- Meta Binary Search | One-Sided Binary Search
- Ternary Search
- Jump Search
- Interpolation Search
- Exponential Search
- Fibonacci Search
- The Ubiquitous Binary Search
Comparisons Between Different Searching Algorithms:
- Linear Search vs Binary Search
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Is Sentinel Linear Search better than normal Linear Search?
Library Implementations of Searching Algorithms:
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Arrays.binarySearch() in Java with examples | Set 1
- Arrays.binarySearch() in Java with examples | Set 2 (Search in subarray)
- Collections.binarySearch() in Java with Examples
Easy Problems on Searching:
- Find the largest three elements in an array
- Find the Missing Number
- Find the first repeating element in an array of integers
- Find the missing and repeating number
- Search, insert and delete in a sorted array
- Count 1’s in a sorted binary array
- Two elements whose sum is closest to zero
- Find a pair with the given difference
- k largest(or smallest) elements in an array
- Kth smallest element in a row-wise and column-wise sorted 2D array
- Find common elements in three sorted arrays
- Ceiling in a sorted array
- Floor in a Sorted Array
- Find the maximum element in an array which is first increasing and then decreasing
- 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 all triplets with zero sum
- Find the element before which all the elements are smaller than it, and after which all are greater
- Find the largest pair sum in an unsorted array
- K’th Smallest/Largest Element in Unsorted Array
- Search an element in a sorted and rotated array
- Find the minimum element in a sorted and rotated array
- Find a peak element
- Maximum and minimum of an array using minimum number of comparisons
- Find a Fixed Point in a given array
- Find the k most frequent words from a file
- Find k closest elements to a given value
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Find the closest pair from two sorted arrays
- Find three closest elements from given three sorted arrays
- Binary Search for Rational Numbers without using floating point arithmetic
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
- K’th Smallest/Largest Element in Unsorted Array | Worst case Linear Time
- K’th largest element in a stream
- Best First Search (Informed Search)
Quick Links:
Recommended: