Posts

The Six Searching Algorithms, at a Glance

Image
Searching is the first real problem-solving skill in Data Structures and Algorithms — and it turns out there isn't just one way to do it. Depending on whether your data is sorted, how it's stored, and even how much you know about the values themselves, a different search strategy wins. This post is your map of all six: what each one does, where it's actually useful, and links into the full breakdown of each with working Java code. Linear search Checks every element one by one until it finds a match. Binary search Repeatedly halves a sorted array to find the target fast. Jump search Skips ahead in fixed blocks, then scans locally to confirm. Interpolation search Estimates the position using the target's actual value. Exponential search Doubles its range to bound an unknown-sized search space. Ternary search Splits the range into three parts instead of two. The Six Searching Algori...

How Ternary Search Works

Every search algorithm this week has split the problem in half. Ternary Search asks an obvious-sounding question: what if we split it into thirds instead? It's a natural extension of Binary Search's core idea — and while it turns out not to beat Binary Search for simple lookups, it introduces a way of thinking about a search space that becomes genuinely powerful once you get to problems like finding a maximum or minimum in a curve — something you'll run into in optimization problems down the line. How Ternary Search Works Pick two midpoints that divide the current range into three roughly equal parts. Compare the target against the values at both midpoints. Based on those two comparisons, eliminate one of the three sections entirely. Repeat on the remaining two-thirds of the range. Walking Through an Example Search for 42 in [4, 8, 15, 16, 23, 38, 42, 56, 72, 91] (indices 0–9): mid1 = index 3 ( 16 ), mid2 = index 6 ( 42 ) 42 == arr[mid2] → match...

How Exponential Search Works

Every search algorithm so far has assumed you know the size of the array before you start. But what if you're searching an unbounded stream — like scrolling through an infinite social media feed, or searching a massive sorted file where you genuinely don't know where it ends? You can't jump to "the middle" of something with no known end. Exponential Search solves exactly this problem: find a range that's guaranteed to contain the target first, then search inside it. How Exponential Search Works Start by checking index 1. If the target is larger than that value, double the index — check index 2, then 4, then 8, then 16, and so on. Stop doubling once you find an index whose value is greater than or equal to the target, or you exceed the array's bounds. You've now found a range — from the last index before doubling to the current one — that must contain the target, if it exists. Run Binary Search inside just that range. Walking Throug...

How Interpolation Search Works

Back to the phone book one more time. Looking for "Zeller"? You don't open to the middle page — you flip almost straight to the back, because you know names starting with Z live near the end. You're not guessing blindly; you're using the actual value you're looking for to estimate its position. That's exactly what Interpolation Search does to sorted numeric data, and when the data is evenly spread out, it's dramatically faster than Binary Search's "always check the middle" approach. How Interpolation Search Works Instead of always checking the middle element like Binary Search, Interpolation Search calculates a smarter guess using a formula based on the target's value relative to the values at both ends of the current range: pos = low + ((target - arr[low]) × (high - low)) / (arr[high] - arr[low]) This formula essentially says: "if the target is close in value to arr[high] , guess a position near the high end. If it's c...

How Jump Search Works

Imagine searching for a word in a printed dictionary, but you're not allowed to flip open to a random page like Binary Search does — you can only jump forward in fixed-size chunks, like flipping ahead 50 pages at a time. Once you've jumped past where the word should be, you go back one chunk and read through it normally. That's Jump Search — a middle ground between checking everything (Linear Search) and repeatedly halving the search space (Binary Search). Why Jump Search Exists Binary Search is fast, but it needs random access to jump straight to any middle index — which works great for arrays, but not as well for data structures where jumping to an arbitrary position is expensive. Jump Search gives you a way to skip through sorted data in bigger strides than Linear Search, without needing the "jump anywhere instantly" ability Binary Search relies on. How Jump Search Works Pick a block size to jump by — typically √n, which turns out to be mathematically ...

How Binary Search Works

Go back to the phone book from Day 2. You're looking for "Martinez." You don't start at "Aaron" and read forward — you flip to the middle, land somewhere around "M," and instantly know whether to search the front half or the back half. Then you do it again on that half. And again. In about 20 flips, you've searched a phone book with a million names. That instinct — cut the problem in half, every single step — is Binary Search, and it's the clearest possible demonstration of why O(log n) beats O(n) so dramatically. The One Requirement: Sorted Data Binary Search only works if the array is already sorted. That sorted order is what lets you safely eliminate half the remaining elements with a single comparison — without it, there's no way to know which half your target is in, and you're back to checking everything with Linear Search. How Binary Search Works Look at the middle element of the array. If it matches the target, yo...

How Linear Search Works

Say you're looking for your friend's name in a stack of unsorted business cards. There's only one honest way to do it: pick up the first card, check it, put it down, pick up the next one, check it, and keep going until you either find the name or run out of cards. No shortcuts, no assumptions about order — just checking everything, one at a time. That's Linear Search, and despite being the simplest algorithm in this entire series, it's also the one you'll reach for most often in real code without even realizing it. How Linear Search Works Start at the first element of the array. Compare it to the value you're looking for. If it matches, you're done — return its position. If not, move to the next element and repeat. If you reach the end without a match, the value isn't in the array. Walking Through an Example Search for 23 in [8, 15, 4, 23, 42, 16] : Check index 0: 8 → no match Check index 1: 15 → no match Check inde...