DSA Searching
Find values efficiently using Linear Search and Binary Search.
Overview
Searching is the process of finding a specific element in a collection such as an array or list. Applications use searching whenever you find a contact, product, email, file, or database record. Choosing an appropriate algorithm becomes especially important with large datasets.
Key concepts
- A successful search usually returns the element's index
- An unsuccessful search indicates that the target does not exist
- Linear Search works with sorted and unsorted data
- Binary Search is faster on large datasets but requires sorted data
What is Searching?
Searching determines whether a target exists in a collection and, when found, returns its location.
Index: 0 1 2 3 4
Array: [15, 25, 35, 45, 55]Searching this array for 35 returns index 2. Searching for 100 reports that the value was not found.
Why is Searching important?
- Finding products in an online store
- Searching mobile contacts
- Locating files on a computer
- Searching an email inbox
- Finding users in a database
- Finding words in a document
Efficient searching helps applications respond quickly and provides a better user experience.
Types of Searching algorithms
Two foundational searching algorithms are Linear Search and Binary Search. Each is suitable for different data and constraints.
Linear Search
Linear Search checks elements one by one until it finds the target or reaches the end. It is simple and works on both sorted and unsorted collections.
const numbers = [12, 25, 8, 40, 19];
const target = 40;
let index = -1;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === target) {
index = i;
break;
}
}
console.log(index); // 3How Linear Search works
- Start at the first element
- Compare it with the target
- Return the index if the values match
- Otherwise move to the next element
- Stop when the target is found or the collection ends
Linear Search complexity
| Case | Complexity |
|---|---|
| Best case | O(1) |
| Average case | O(n) |
| Worst case | O(n) |
Linear Search is easy to implement, but it becomes slower as the collection grows.
Binary Search
Binary Search repeatedly divides the search area in half. It is substantially faster for large collections, but the data must already be sorted.
const numbers = [10, 20, 30, 40, 50, 60, 70];
const target = 50;
let left = 0;
let right = numbers.length - 1;
let index = -1;
while (left <= right) {
const middle = Math.floor((left + right) / 2);
if (numbers[middle] === target) {
index = middle;
break;
}
if (numbers[middle] < target) {
left = middle + 1;
} else {
right = middle - 1;
}
}
console.log(index); // 4How Binary Search works
- Check the middle element
- If the target is larger, discard the left half
- If the target is smaller, discard the right half
- Repeat until the target is found or no search area remains
Because each step cuts the remaining area in half, Binary Search needs relatively few comparisons even for very large collections.
Binary Search complexity
| Case | Complexity |
|---|---|
| Best case | O(1) |
| Average case | O(log n) |
| Worst case | O(log n) |
Linear Search vs Binary Search
| Feature | Linear Search | Binary Search |
|---|---|---|
| Works on unsorted data | Yes | No |
| Requires sorted data | No | Yes |
| Implementation | Easy | Moderate |
| Large datasets | Slower | Faster |
| Typical time | O(n) | O(log n) |
When data is already sorted, Binary Search is usually the better choice. Linear Search remains useful for small or unsorted collections.
Real-life example
Imagine finding a name in a printed phone directory. Linear Search begins on the first page and checks each name. Binary Search uses the alphabetical order: open near the middle, discard the half that cannot contain the name, and repeat. The second approach is much faster in a large directory.
Where is Searching used?
- Search engines
- E-commerce websites
- Banking applications
- Hospital management systems
- Student record systems
- Navigation apps
- Music and video platforms
- File management software
Tips for beginners
- Begin with Linear Search
- Learn Binary Search after understanding sorted arrays
- Draw the changing Binary Search boundaries
- Compare algorithm time complexities
- Practice the same problem with both approaches
Key takeaways
- Searching locates an element within a collection
- Linear Search examines values one by one
- Binary Search halves a sorted search space
- Linear Search is typically O(n)
- Binary Search is typically O(log n)
- Choosing the right search can significantly improve application performance