Essential Algorithms Every Software Developer Should Master
The article systematically explains the most common algorithms—search, sorting, two‑pointer, hash, recursion, graph traversal, greedy and dynamic programming—detailing their principles, typical use cases, real‑world code examples, and practical advice for when to apply or avoid them in everyday software development.
Introduction
The core idea is that an algorithm is a reusable code template for solving a specific problem. Algorithms are not limited to AI; they are used daily in backend, web, and app development.
1. Search Algorithms (most frequent in daily development)
1.1 Sequential (Linear) Search
Explanation: Scan elements from start to end until the target is found; stop early if found, otherwise traverse the whole collection.
Applicable scenarios: Unsorted arrays or List collections.
Real‑world case: Backend iterates an unsorted user list to find a matching phone number; frontend scans a dropdown array for the selected item.
Limitation: Very slow on large data sets (e.g., 100,000 items may require 100,000 comparisons in the worst case).
1.2 Binary (Half‑interval) Search (interview focus)
Prerequisite: Data must be sorted.
Process: Compare the middle element with the target; discard the half that cannot contain the target and repeat.
Typical scenarios: Fast lookup in ordered data.
Real‑world cases: Finding a price in a sorted product list; locating an order by date in a time‑sorted list; underlying principle of MySQL indexes.
Example: In a sorted array [10,20,30,40,50], searching for 40 examines 30 first, discards the left half, and continues with the right half.
Key limitation: Cannot be used on unsorted arrays.
2. Sorting Algorithms (ubiquitous in business development)
2.1 Bubble Sort
Compares adjacent elements and bubbles the larger one toward the end; suitable only for very small data sets (tens of items). Practically never hand‑written in projects.
2.2 Quick Sort (most classic)
Explanation: Choose a pivot, partition elements smaller than the pivot to the left and larger to the right, then recursively sort the partitions.
Scenario: General‑purpose sorting; the core idea behind Java Arrays.sort.
Case: Backend sorts products by sales amount or orders by creation time.
2.3 Merge Sort
Recursively split the array in half until single elements remain, then merge pairs in order. Stable but requires extra memory; used for external sorting of large data sets or file content.
2.4 Heap Sort
Based on a heap data structure; Java PriorityQueue is a heap implementation. Example: generating a leaderboard of top‑10 best‑selling products or top‑20 hottest articles.
Development tip: In practice, developers rely on language‑provided utilities like Collections.sort rather than hand‑writing these algorithms, but understanding their principles helps choose the right tool and avoid inefficient custom loops on large data.
3. Two‑Pointer Algorithms (backend, LeetCode, high‑frequency business scenarios)
Concept: Use two indices (e.g., one at the start, one at the end) that move toward each other or in tandem to reduce loop iterations.
Use cases: Processing ordered arrays, removing duplicates, computing range sums.
Example 1 (head‑tail pointers): Find two numbers in a sorted array whose sum equals a target (common requirement for amount combination queries).
Example 2 (fast‑slow pointers): Detect a cycle in a linked list or locate the middle node.
4. Hash Algorithms (used every day, critical)
Idea: Convert any input (text or object) into a fixed‑length numeric hash value; identical inputs always produce the same hash, tiny changes produce completely different hashes.
Scenario 1 – Password hashing: Store only the hash (e.g., SHA‑256 with salt) instead of plaintext; during login, hash the input and compare.
Scenario 2 – Hash tables (HashMap, HashSet): Provide O(1) lookup speed. Example: batch‑load users into a HashMap for rapid ID‑based retrieval, avoiding repeated scans.
Scenario 3 – File deduplication: Compute a file’s hash on upload to detect duplicates (the “instant upload” technique used by cloud storage).
5. Recursion & Divide‑and‑Conquer
5.1 Recursion
A method calls itself with a base case to stop; otherwise it leads to infinite loops and stack overflow.
Case 1 – Tree traversal: Rendering hierarchical menus or product categories with unlimited depth.
Case 2 – Directory walk: Recursively enumerate all files in a folder.
Pitfall: Deep recursion can overflow the stack; for very deep hierarchies, prefer iterative approaches.
5.2 Divide‑and‑Conquer
Break a big problem into independent sub‑problems, solve each, then combine results. Quick sort and merge sort are classic examples.
6. Depth‑First Search (DFS) & Breadth‑First Search (BFS)
DFS
Explore a path to its end before backtracking.
Backend recursive query of unlimited‑level categories (tree menus).
File system directory traversal.
BFS
Visit nodes level by level, expanding outward.
Social network: find N‑degree friends.
Maze pathfinding.
Network topology shortest‑path calculations.
Web crawler: fetch all links on the current page before moving to the next depth.
Comparison: DFS goes deep (potentially inefficient for shortest path), BFS expands uniformly and is suitable for shortest‑path problems.
7. Greedy Algorithms
At each step choose the locally optimal option, hoping to reach a global optimum. Used for resource allocation and interval selection.
Scheduling meetings to maximize non‑conflicting events.
Partial change‑making problems.
Bandwidth allocation.
Limitation: Not guaranteed to produce the global optimum in all scenarios; dynamic programming may be required.
8. Dynamic Programming (DP) – interview hot spot, moderate business use
Decompose a large problem into overlapping sub‑problems, store intermediate results (memoization) to avoid redundant computation—trading space for time.
Knapsack problem: Optimize cargo loading under weight limits (logistics, warehousing).
Longest common substring: Document comparison and plagiarism detection.
Coupon combination: Compute the minimum order amount after applying multiple discounts.
Note: Simple CRUD applications rarely need custom DP; it appears more in big‑data, billing, or scheduling systems.
9. Common String Algorithms
KMP (Knuth‑Morris‑Pratt)
Efficient pattern matching to determine whether a long text contains a target keyword. Used in sensitive‑word filtering and search engines; Java’s String.contains hides a simplified version.
Sliding Window (derived from two‑pointer)
Maintain a fixed or variable‑size window that slides over a string or array to aggregate statistics.
Find the longest substring without repeating characters.
Calculate total order amount within a continuous time window.
10. Graph Shortest‑Path Algorithms (medium‑to‑large projects, map services)
Dijkstra’s Algorithm
Computes the shortest distance between two points.
Map software: determine the fastest route from start to destination.
Logistics: plan the shortest delivery path.
Top 8 Algorithms for Everyday CRUD Development
Hash algorithms (HashMap, encryption, deduplication)
Binary search (ordered data lookup)
Two‑pointer techniques (array handling)
Quick sort (general sorting)
DFS (tree structures: menus, categories)
BFS (layered traversal, shortest path)
Sliding window
Recursion
Practical Advice from Real‑World Experience
~90 % of typical business systems (admin panels, mini‑program backends) do not require hand‑written complex algorithms; the JDK already provides optimized sorting, searching, and hashing utilities.
Focus on selecting the right container ( List, Map, Set) and leveraging built‑in algorithms instead of writing inefficient double loops that time out on tens of thousands of records.
When to Design Your Own Algorithm?
Large‑scale data processing, scheduling, routing, billing, risk control, search engines, AI.
Massive data where ordinary loops are insufficient; optimization becomes mandatory.
Common Pitfall for Beginners
Using nested for loops for large data sets leads to interface timeouts. Optimizations include employing HashMap (hashing), binary search, or pre‑sorting data.
Clear Comparison Examples
Finding an order number in a sorted list of 100 000 entries:
Sequential search: up to 100 000 comparisons.
Binary search: only dozens of comparisons, dramatically faster.
Retrieving all sub‑categories of a three‑level product hierarchy:
DFS recursion walks to the deepest leaf.
Storing user passwords securely:
Apply SHA‑256 with a salt (hash algorithm).
Getting the top‑10 best‑selling products:
Use a heap‑based priority queue (heap sort concept).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
CTO Full-Stack Academy
15 years of IT industry experience, sharing practical insights on pre-sales, product design, architecture, technology development, software testing, project management, IT consulting, and operations management.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
