Algorithms

An algorithm is a precise, finite sequence of steps for transforming an input into an output, and the choice of algorithm — not just the choice of data structure from the previous topic — is what decides whether a program finishes in milliseconds or never finishes at all. Searching is the simplest case: a linear search checks every element one at a time and works on any array, but a binary search exploits a sorted array's ordering by repeatedly checking the middle element and discarding half the remaining range, the same divide-in-half trick a binary search tree already performs structurally. Sorting rearranges an array into order so that binary search and other ordering-dependent operations become possible at all; a sorting visualizer — one of this series' project ideas — makes the contrast between approaches visible directly, since a simple approach like bubble sort repeatedly swaps adjacent out-of-order pairs in a slow quadratic crawl, while a divide-and-conquer approach like merge sort splits the array in half, recursively sorts each half, and merges the two sorted halves back together in linear time, finishing in a fraction of the comparisons. Recursion is a function calling itself on a smaller version of the same problem until it reaches a base case simple enough to answer directly, and it is exactly how merge sort's "split in half" step is actually implemented, with each recursive call consuming its own frame on the call stack this series already covered under memory and program execution. Graph traversal walks a graph's nodes and edges systematically: breadth-first search explores everything one step away before moving two steps away, guaranteeing the shortest path in an unweighted graph, while depth-first search commits to one path as far as it can go before backtracking — a maze solver is depth-first search applied directly to a grid where walls block edges, and a route finder layers in edge weights (distance or time) to find not just any path but the cheapest one. A greedy method builds a solution by always taking the locally best-looking choice at each step and never reconsidering it, which is fast but only provably correct for problems with the right structure; dynamic programming instead breaks a problem into overlapping subproblems and stores each subproblem's answer the first time it is computed so that every later reference to the same subproblem is a lookup instead of a recomputation, turning an exponential brute-force search into a polynomial one. Every one of these six tools — search, sort, recursion, traversal, greedy, dynamic programming — is a direct answer to a specific problem shape, which is why the next topic in this series, algorithm analysis, exists to measure exactly how much each one actually costs.

What shape is the problem?
find a value, order a list, or navigate connections
Search / sort
linear vs. binary search, bubble vs. merge sort
Recursion
function calls itself on a smaller subproblem
Graph traversal
BFS shortest path, DFS maze solving
Greedy / dynamic programming
locally-best choice vs. cached subproblem reuse
Pick the technique that matches the problem's structure
algorithm analysis (next) measures what each one actually costs
Sorting Visualizer: Bubble Sort vs. Merge Sort Same unsorted array, two very different step counts Bubble sort — one swap at a time 5 2 4 1 5 greater than 2: swap one adjacent pair fixed per pass quadratic: n times n comparisons Merge sort — split, sort, merge 5 2 4 1 2 5 1 4 1 2 4 5 recursive split then linear merge back together bubble sort: n squared comparisons as the array grows merge sort: n times log(n), the gap widens fast on large arrays a sorting visualizer makes exactly this swap-count gap visible on screen
Graph Traversal: a Maze as a Grid of Nodes Breadth-first search guarantees the shortest route start 1 2 wall 1 wall 3 4 2 3 4 goal numbers show BFS's step layer: step 1 explores every cell one hop from start, step 2 explores every cell two hops away, and so on until the goal is reached maze solver = DFS or BFS walls simply mean that edge does not exist in the graph DFS: commit to one path, backtrack on dead end BFS: explore layer by layer, shortest path found first route finder adds weights each edge carries a distance or time cost, so the cheapest path can differ from the path with the fewest hops same traversal idea, grid cells or road intersections, walls or distances

Take algorithms further

Ask the copilot about this term, or have our engineers assess it against your process.