Searching in JAVA
Searching in JAVA
md 2024-08-21
Algorithms - Searching
1. Introduction to Searching Algorithms
Searching algorithms are designed to retrieve information stored within some data structure or database.
These algorithms aim to locate the position of a particular item in a collection of items. Understanding
searching algorithms is crucial for optimizing performance in tasks involving large data sets.
Efficient Data Retrieval: Searching helps in quickly locating specific data, which is essential in large
datasets.
Application in Various Fields: From databases to file systems and even in algorithms like AI, searching
plays a key role.
Foundation for More Complex Algorithms: Understanding basic search algorithms lays the
groundwork for learning more complex algorithms like sorting, pathfinding, etc.
2. 🔍 Linear Search
2.1 Concept of Linear Search
Linear Search is the simplest searching algorithm that checks every element in a list sequentially until the
desired element is found or the list ends.
Advantages:
Disadvantages:
Algorithm:
Pseudocode:
1 / 10
Searching.md 2024-08-21
Time Complexity:
Write a program to find the first occurrence of a number in an array using linear search.
Variations:
3. 🔍 Binary Search
3.1 Concept of Binary Search
Binary Search is an efficient algorithm that works on sorted arrays by dividing the search interval in half. The
idea is to eliminate half of the elements from the list at each step, thereby reducing the search time.
Advantages:
Disadvantages:
2 / 10
Searching.md 2024-08-21
Algorithm:
Pseudocode:
Time Complexity:
Space Complexity:
Handling Duplicates: Modify the algorithm to find the first or last occurrence of a duplicate element.
Search in Infinite Arrays: Adjust the boundaries dynamically to accommodate an infinite array.
Searching in Rotated Sorted Arrays: Account for the rotation and search accordingly.
Recursive Binary Search involves calling the function within itself with updated bounds, while Iterative
Binary Search uses a loop to adjust bounds without recursion.
3 / 10
Searching.md 2024-08-21
Point of
Recursive Binary Search Iterative Binary Search
Comparison
Implementation Uses recursion to divide the search space Uses loops to divide the search space
Space
O(log n) due to the call stack O(1), as no extra space is needed
Complexity
Variations:
Time Complexity O(n) in all cases O(log n) for average and worst cases
4 / 10
Searching.md 2024-08-21
Performance on Large Inefficient due to linear time Highly efficient due to logarithmic time
Datasets complexity complexity
Linear Search: Best used when dealing with small datasets or when the data isn't sorted.
Binary Search: Preferred when working with large, sorted datasets for faster search operations.
5. Conclusion
Searching algorithms are crucial for efficient data retrieval. Linear search is simple but slow, while binary
search is fast but requires sorted data. Understanding these algorithms helps in choosing the right one based
on the specific use case, data size, and requirements.
6. Additional Resources
Books: "Introduction to Algorithms" by Cormen et al., "Algorithms Unlocked" by Thomas H. Cormen
Articles: Check out blogs and articles on GeeksforGeeks, Medium, etc.
Practice Platforms: LeetCode, HackerRank, Codeforces for solving search algorithm problems.
Jump Search is an algorithm designed for sorted arrays. It works by jumping ahead by a fixed number of
steps, rather than checking each element sequentially like in Linear Search.
How it Works:
5 / 10
Searching.md 2024-08-21
Advantages:
Disadvantages:
Complexity:
Pseudocode:
2. Interpolation Search
Interpolation Search is an improvement over binary search for instances where the values in a sorted array
are uniformly distributed.
How it Works:
Instead of using the middle element, it estimates the position of the target based on its value
relative to the start and end elements.
The formula used to estimate the position is: [ \text{pos} = \text{low} + \left(\frac{(\text{target} -
\text{arr[low]})}{(\text{arr[high]} - \text{arr[low]})}\right) \times (\text{high} - \text{low}) ]
The search continues like binary search, adjusting the low and high bounds.
Advantages:
Disadvantages:
Complexity:
Pseudocode:
3. Exponential Search
Exponential Search is used for unbounded or infinite arrays. It starts with a small range and exponentially
increases the range until it finds the target or a range where the target might exist, then switches to binary
search.
How it Works:
Advantages:
Disadvantages:
Complexity:
7 / 10
Searching.md 2024-08-21
Pseudocode:
4. Fibonacci Search
Fibonacci Search is similar to binary search but uses Fibonacci numbers to divide the array into sections. It is
useful for optimizing searches in large datasets.
How it Works:
Advantages:
Disadvantages:
Complexity:
Pseudocode:
8 / 10
Searching.md 2024-08-21
offset = -1
while (fibM > 1):
i = min(offset + fib2, length(arr) - 1)
if arr[i] < target:
fibM = fib1
fib1 = fib2
fib2 = fibM - fib1
offset = i
else if arr[i] > target:
fibM = fib2
fib1 = fib1 - fib2
fib2 = fibM - fib1
else:
return i
if fib1 and arr[offset + 1] == target:
return offset + 1
return -1
5. Ternary Search
Ternary Search is a divide-and-conquer algorithm similar to binary search, but it divides the array into three
parts instead of two.
How it Works:
Advantages:
Disadvantages:
Complexity:
Pseudocode:
9 / 10
Searching.md 2024-08-21
if arr[mid1] == target:
return mid1
if arr[mid2] == target:
return mid2
if target < arr[mid1]:
return ternarySearch(arr, target, low, mid1-1)
else if target > arr[mid2]:
return ternarySearch(arr, target, mid2+1, high)
else:
return ternarySearch(arr, target, mid1+1, mid2-1)
return -1
These additional searching algorithms are more specialized and optimized for certain scenarios.
10 / 10