Posts

Showing posts from July, 2026

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...

Strings in Java: How They Actually Work in Memory

Here's a question that trips up even developers with a few years of experience: if you concatenate strings inside a loop 10,000 times, why does your code suddenly get slow — even though each individual + looks like a tiny, cheap operation? The answer is the single most important thing to understand about strings in Java, and it comes down to one word: immutability . We covered Arrays last time as contiguous memory you can freely change. Strings look similar on the surface — they're basically a sequence of characters — but Java treats them very differently under the hood, and that difference is exactly what today's post is about. A String is Secretly a char Array At the lowest level, a Java String is backed by a character array (technically a byte array since Java 9's "compact strings," but the concept is the same). So why not just use char[] directly? Because String adds one critical guarantee on top: once created, its contents can never change . Wh...

Arrays: The Data Structure Everything Else Builds On

Imagine an apartment building where every unit is the exact same size, numbered in a straight line starting from 0, and the building manager keeps a master list of exactly where each unit is. Need unit 47? You don't walk the whole building counting doors — you go straight there, because the building's layout tells you exactly how far to go. That's an array. It's the simplest data structure there is, and it's also the one every other data structure in this series will eventually be compared against. We touched on Big O last time in the abstract — today you'll see exactly why arrays get their reputation for being fast at some things and slow at others, and why that reputation is really about memory, not magic. What is an Array? An array is a collection of elements, all the same type, stored in contiguous memory — meaning right next to each other, back to back, with no gaps. Each element has an index , starting at 0, that tells the computer exactly how far to...

Common Time Complexities Explained

Common Time Complexities Explained Let's understand the most common time complexities with simple Java examples and real-life analogies. 1. O(1) – Constant Time An algorithm runs in constant time if the number of operations remains the same regardless of the input size. Real-Life Example Finding the first page of a book. Whether the book has 100 pages or 10,000 pages, opening the first page always takes one step. Java Example int[] numbers = {10, 20, 30, 40, 50}; System.out.println(numbers[0]); Complexity Time Complexity: O(1) Space Complexity: O(1) Interview Tip Array indexing is one of the best examples of O(1). 2. O(log n) – Logarithmic Time The problem size is reduced by half after every step. Real-Life Example Searching a word in a dictionary. You don't read every page. Instead, you repeatedly open near the middle. Java Example int low = 0; int high = arr.length - 1; while(low <= high){ int mid = (low + high) / 2; ...

Time and Space Complexity Explained with Java Examples | Complete Beginner's Guide

Time and Space Complexity Explained with Java Examples | Complete Beginner's Guide If you've ever wondered why one program runs faster than another, or why two solutions to the same problem can have very different performance, the answer often lies in Time Complexity and Space Complexity . Understanding complexity analysis is one of the most important skills for every programmer. Whether you're preparing for coding interviews, building scalable software, or simply writing better code, learning complexity analysis will help you choose the right algorithm for the job. In this tutorial, we'll explain Time and Space Complexity from the ground up using simple language, real-world examples, and Java programs. 📚 Table of Contents What is Complexity Analysis? Why Time Complexity Matters What is Time Complexity? Real-Life Analogy How We Measure Time Complexity Big O Notation Common Time Complexities Space Complexity Java Examples Inter...

What is Data Structures and Algorithms (DSA)? A Complete Beginner's Guide with Java

Every developer hits the same wall eventually: you can write code that works, but interviewers keep asking about "time complexity," senior engineers keep saying "just use a HashMap here," and your code slows to a crawl the moment real data shows up. The missing piece is almost always Data Structures and Algorithms (DSA) — and this post is where you start closing that gap. This is the opening article in the CodeElevateX DSA Series . By the end, you'll know exactly what DSA means, why it matters, where it shows up in software you already use, and how the rest of this series is going to take you from zero to interview-ready — using Java. What is DSA? DSA stands for Data Structures and Algorithms — two ideas that always work as a pair. A Data Structure is a way to organize and store data efficiently. An Algorithm is a step-by-step procedure for solving a problem using that data. Think of a library with thousands of books: How the books are arranged o...

Bubble Sort in Java — Explained Simply with Code

Imagine you're handed a shuffled deck of 5 playing cards and told to sort them — but there's a rule: you can only compare two cards next to each other at a time, and swap them if they're in the wrong order. You keep walking down the line, swapping out-of-order pairs, again and again, until one full pass goes by with no swaps at all. That's it — that's Bubble Sort. It's not the fastest algorithm you'll ever write. But it's the one that teaches you how sorting actually works under the hood — and once it clicks, every other sorting algorithm on this blog will make a lot more sense. Why "Bubble" Sort? Picture the numbers in your array as bubbles in a glass of soda. Every time a bigger bubble is below a smaller one, they swap places — so with each pass, the biggest remaining "bubble" floats up to its correct spot at the end of the list. Do enough passes, and eventually every bubble has floated to exactly where it belongs. 📝 No...