AlgoLab
Every algorithm here runs on data you choose and records each step it takes, so you can stop anywhere, go backwards, and read what it was doing. Pick one.
29 algorithms
Sorts
Putting things in order, and what each way costs.
- Bubble sortRepeatedly swaps adjacent out-of-order pairs. Simple to reason about, rarely the right choice once n grows past a handful of items.O(n²)O(1) spacestable
- Selection sortScans the unsorted remainder for the smallest value and swaps it into place. Always makes the same number of comparisons, no matter the input order.O(n²)O(1) spacenot stable
- Insertion sortGrows a sorted prefix one value at a time, shifting larger values right to make room. Fast on data that is already nearly sorted.O(n²)O(1) spacestable
- Merge sortSplits the array in half recursively, then merges each pair of sorted halves back together. Predictable O(n log n) performance regardless of input order.O(n log n)O(n) spacestable
- QuicksortPicks a pivot, partitions the range around it, then recurses on each side. Fast in practice, but a poorly chosen pivot on already-ordered data is its worst case.O(n log n)O(log n) spacenot stable
- Heap sortBuilds a max-heap in the array, then repeatedly moves the largest remaining value to its final position. Guaranteed O(n log n), in place, but not stable.O(n log n)O(1) spacenot stable
- Shell sortRuns insertion sort on far-apart pairs first, shrinking the gap each pass until it reaches one. The gap sequence is the whole idea, and the bridge from insertion sort to the faster sorts.O(n^1.3)O(1) spacenot stable
- Counting sortCounts how many times each value occurs, then reconstructs the sorted array straight from those counts. Not a comparison sort at all — it never asks whether one value is greater than another.O(n + k)O(n + k) spacenot stable
- Radix sort (LSD)Sorts numbers one digit at a time, from the ones place upward, using a stable counting sort at every digit. Like counting sort, it never compares two values directly.O(d(n + b))O(n + b) spacestable
- Cocktail shaker sortBubble sort that alternates direction each pass, carrying the smallest remaining value to the front just as quickly as the largest goes to the end.O(n²)O(1) spacestable
- Comb sortBubble sort with a shrinking comparison gap, starting wide and narrowing to one. Solves bubble sort's small-value problem differently to cocktail shaker sort, and previews shell sort's gap idea.O(n²)O(1) spacenot stable
- Gnome sortInsertion sort reduced to a single index and no inner loop: step forward when in order, swap and step back when not. The simplest correct sort there is.O(n²)O(1) spacestable
- Cycle sortWrites every value directly into its final position exactly once — provably the minimum number of writes of any comparison sort. The sharpest possible contrast with selection sort on the same input.O(n²)O(1) spacenot stable
- Binary insertion sortInsertion sort that finds each value's insertion point by binary search instead of a linear scan. Cuts comparisons sharply; the shift count — and so the number of writes — does not change at all.O(n²)O(1) spacestable
- Bucket sortDistributes values into a fixed number of ranges, sorts each range with insertion sort, then concatenates them in order. Counting sort and radix sort are both special cases of this idea.O(n + k)O(n + k) spacestable
Searches
Finding a value, and what you have to know about the data first.
- Linear searchChecks every value in order. Works on any array, sorted or not, but is slow for large arrays.O(n)O(1) spaceneeds sorted
- Binary searchRepeatedly checks the midpoint of the remaining range and discards the half that cannot hold the target. Requires the array to be sorted first.O(log n)O(1) spaceneeds sorted
- Jump searchChecks the end of fixed-size blocks to find the block that could hold the target, then scans that block. Faster than linear search, simpler than binary search.O(√n)O(1) spaceneeds sorted
- Interpolation searchEstimates where the target should be, proportional to its value between the low and high bounds, instead of always checking the midpoint. Excellent on uniform data, and can degrade badly on clustered data.O(log log n)O(1) spaceneeds sorted
- Exponential searchDoubles a bound until it passes the target, then binary searches inside that bracket. At the sizes this tool supports, the doubling itself is only a handful of probes — the idea worth watching is finding a bracket before halving it.O(log n)O(1) spaceneeds sorted
- Ternary searchSplits the remaining range into thirds instead of halves. It performs more comparisons than binary search, not fewer — a direct, checkable surprise, not a faster alternative.O(log n)O(1) spaceneeds sorted
- Fibonacci searchNarrows a sorted range the way binary search does, but chooses each split point from a Fibonacci number instead of halving — using only addition and subtraction, never division.O(log n)O(1) spaceneeds sorted
AI-era algorithms
How a language model turns text into tokens, how every token is scored against every other, how the next one is picked, how a cache avoids redoing that scoring at every step, and how several tokens can be proposed cheaply and verified together instead of one at a time. These run no model: every number comes from data written by hand and shown in full inside the tool.
- Beam searchKeeps the best few partial sequences instead of only the best one, so a strong continuation behind a weak first token is still reachable. Greedy decoding is this with the beam set to one.O(k · B · D)O(k · D) spaceauthored data
- Byte-pair encodingBuilds a vocabulary by repeatedly merging the most frequent pair of neighbouring tokens, starting from single characters. This is how a language model gets its tokens, and why it splits words in places that look arbitrary.O(m · n)O(n + m) spaceauthored data
- Scaled dot-product attentionScores every token against every other with a dot product, turns each row of scores into weights that add up to one, and builds each output as those weights applied to the value vectors. Four numbers per token, typed in by hand, so the arithmetic can be checked by eye.O(n² · dₖ)O(n²) spaceauthored data
- Softmax, temperature, top-k and top-pTurns fifty authored logits into probabilities with softmax, shows what dividing by a temperature does to the shape of that distribution, then narrows it two ways: keep only the k highest-probability tokens, or keep the smallest group whose probabilities add up to p. Three hand-authored distributions, so the effect of each control is a measured number rather than a claim.O(n log n)O(n) spaceauthored data
- KV cache and context windowGenerates a hand-authored, twelve-token sequence one token at a time and computes the cost of two ways to do it: recomputing every key and value vector from scratch at every step, or caching them and only computing the new one. Shows what happens once the cache reaches its capacity — the oldest entries are evicted, a sliding context window — and computes, rather than asserts, how the total cost grows in each case.O(n), with a fixed-capacity cacheO(w) spaceauthored data
- Speculative decodingProposes several tokens ahead with a cheap draft distribution, then verifies each one in turn against the target distribution it would otherwise have produced one token at a time — accepting while a fixed probability rule is satisfied, resampling the first rejected position from what the rule leaves over, and discarding everything proposed after it. Computes, rather than asserts, that the output this produces has exactly the same distribution as generating one token at a time with the target alone.between O(1) and O(k) target steps for k tokens, depending on how often a proposal is acceptedO(k) spaceauthored data
- The ReAct loopReplays one of three pre-written transcripts through a fixed state machine: a Thought entry, then an Action entry, then an Observation entry, repeating until an Action entry is a finish action or a fixed turn limit is reached. Draws the current state, the transitions taken, and an append-only log of every entry shown so far — none of it generated while the page runs.O(1)O(1) spaceauthored data
Nothing matches that. Clear the filter to see all 29.