Searching Algorithms
Last Updated :
15 May, 2025
Improve
Searching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input array.
- Linear Search : It is used for an unsorted array. It mainly does one by one comparison of the item to be search with array elements. It takes linear or O(n) Time.
- Binary Search : It is used for a sorted array. It mainly compares the array's middle element first and if the middle element is same as input, then it returns. Otherwise it searches in either left half or right half based on comparison result (Whether the mid element is smaller or greater). This algorithm is faster than linear search and takes O(Log n) time.
One more common search technique is Two Pointers Technique where we begin searching from both ends of the array.
Library Implementations of Binary Search
- binary_search, lower_bound and upper_bound in C++
- Arrays.binarySearch() in Java
- Arrays.binarySearch() in Java for Search in subarray
- Collections.binarySearch() in Java
- Bisect in Python
- List.BinarySearch in C#
Easy Problems
- Largest in an Array
- Second Largest in an array
- Largest three in an array
- Missing Number
- First Repeating
- Missing and Repeating
- Count 1’s in a sorted binary array
- Closest to 0 Sum Pair
- Pair with the given difference
- k largest(or smallest) Elements
- Kth smallest in row and column-wise sorted
- Common elements in 3 sorted
- Ceiling in a sorted
- Floor in a Sorted
- Maximum in a Bitonic
- Elements that appear more than n/k times
Medium Problems
- Triplets with zero sum
- Partition Point
- Largest pair sum
- K’th Smallest in Unsorted Array
- Search an in a sorted and rotated
- Min in a sorted and rotated
- Max in a sorted and rotated
- Peak element
- Max and min using minimum comparisons
- Find a Fixed Point in a given array
- K most frequent words from a file
- K closest elements
- 2 Sum – Pair Sum Closest to Target in Sorted Array
- Closest pair from two sorted arrays
- Three closest from three sorted arrays
- Binary Search for Rationals
- Missing Element in AP
Hard Problems
- Median of two sorted arrays
- Median of two sorted of different sizes
- Search in an almost sorted array
- Search in a sorted infinite array
- Pair sum in a sorted and rotated array
- K’th Smallest/Largest Element in Unsorted Array
- K’th largest element in a stream
- Best First Search (Informed Search)
More Searching Algorithms
- Sentinel Linear 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?
Quick Links:
- ‘Practice Problems’ on Searching
- Top Searching Interview Questions
- ‘Quizzes’ on Searching
- Learn Data Structure and Algorithms | DSA Tutorial