Play, pause, and step through classic algorithms — with real source code in multiple languages, right in your browser.
50algorithms
9categories
4languages
Bubble Sort
Repeatedly steps through the list, swapping adjacent out-of-order pairs so the largest values 'bubble' to the end.
avg O(n²)space O(1)
Selection Sort
Finds the minimum of the unsorted region on each pass and places it at the front.
avg O(n²)space O(1)
Insertion Sort
Builds the sorted array one item at a time by inserting each new element into its correct place among the already-sorted prefix.
avg O(n²)space O(1)
Quick Sort
Divide and conquer: pick a pivot, partition values around it, then recursively sort each side.
avg O(n log n)space O(log n)
Merge Sort
Divide and conquer: split the array in half, sort each half, then merge the two sorted halves.
avg O(n log n)space O(n)
Heap Sort
Builds a max-heap, then repeatedly swaps the root (largest) to the end and restores the heap — in-place and O(n log n) in every case.
avg O(n log n)space O(1)
Shell Sort
A gap-based generalization of insertion sort: sort elements far apart first, shrinking the gap until it becomes a final pass of ordinary insertion sort.
avg O(n^1.25)space O(1)
Cocktail Shaker Sort
A bidirectional bubble sort: each round bubbles the largest to the right, then the smallest to the left, closing in from both ends.
avg O(n²)space O(1)
3-Way Quicksort
Dijkstra's Dutch national flag partition splits into <, = and > the pivot in one scan, so duplicate keys finish immediately.
avg O(n log n)space O(log n)
Counting Sort
Sorts without a single comparison by tallying how often each key appears, then writing the keys back in order — O(n + k).
avg O(n + k)space O(k)
Radix Sort
Sorts one digit at a time from least significant to most, using a stable bucket pass per digit — O(d · n), no comparisons.
avg O(d · (n + b))space O(n + b)
Linear Search
Checks each element in turn until the target is found or the array ends. Works on any array, sorted or not.
avg O(n)space O(1)
Binary Search
On a sorted array, repeatedly halves the search range by comparing the middle element to the target — O(log n).
avg O(log n)space O(1)
Jump Search
On a sorted array, jumps ahead by fixed blocks of √n until it passes the target, then scans the last block linearly.
avg O(√n)space O(1)
Interpolation Search
Like binary search, but estimates the probe position from the target's value — near O(log log n) on uniformly distributed data.
avg O(log log n)space O(1)
Exponential Search
Doubles an index bound until it overshoots the target, then binary-searches the found range — great for unbounded or very large sorted inputs.
avg O(log n)space O(1)
Ternary Search
Splits the range into thirds with two probes per round — asymptotically the same as binary search, but with more comparisons.
avg O(log₃ n)space O(1)
Fibonacci Search
Narrows the range by Fibonacci numbers instead of halves, so every probe is computed with additions only — no division.
avg O(log n)space O(1)
Breadth-First Search
Explores the grid in expanding rings from the start, guaranteeing the fewest-steps path on an unweighted grid.
avg O(V + E)space O(V)
Depth-First Search
Plunges as deep as possible along one branch before backtracking. Finds a path, but not necessarily the shortest.
avg O(V + E)space O(V)
Dijkstra's Algorithm
Expands the closest unsettled node first using a priority queue, giving the shortest path even when cells have different costs (weights).
avg O(E log V)space O(V)
A* Search
Like Dijkstra, but a heuristic pulls the search toward the goal — so it settles far fewer cells while still finding the shortest path.
avg O(E log V)space O(V)
Greedy Best-First Search
Always expands the node that looks closest to the goal by heuristic alone. Very fast and targeted, but its path is not guaranteed to be shortest.
avg O(E log V)space O(V)
Longest Common Subsequence
Fills a table where dp[i][j] is the LCS length of the first i and j characters — each cell built from its diagonal, top, or left neighbour.
avg O(m·n)space O(m·n)
Edit Distance
The Levenshtein distance: the minimum number of insertions, deletions, or substitutions to turn one string into another, built cell by cell.
avg O(m·n)space O(m·n)
Longest Common Substring
The same table as LCS with one rule flipped: a mismatch resets the cell to 0, because a substring has to be contiguous.
avg O(m·n)space O(m·n)
0/1 Knapsack
Pack the most valuable subset within a weight limit. Each cell asks one question: skip this item, or take it?
avg O(n·W)space O(n·W)
Coin Change (DP)
The fewest coins for an amount — and the table that proves the greedy answer can be wrong.
avg O(m·A)space O(m·A)
Inorder Traversal
Visits Left → Node → Right. On a binary search tree this yields the values in sorted order.
avg O(n)space O(n)
Preorder Traversal
Visits Node → Left → Right. Useful for copying a tree or serialising its structure.
avg O(n)space O(n)
Postorder Traversal
Visits Left → Right → Node. Children are processed before their parent — handy for deleting or evaluating a tree.
avg O(n)space O(n)
Level-order Traversal
Breadth-first: visits the tree one level at a time, left to right, using a queue.
avg O(n)space O(n)
Zigzag Level-order
Breadth-first but alternating: the first row goes left to right, the next right to left, spiralling down the tree.
avg O(n)space O(n)
BST Search
Walks down from the root, going left or right by comparing the target to each node — O(log n) on a balanced tree.
avg O(log n)space O(1)
N-Queens
Places N queens on an N×N board so none share a row, column, or diagonal — trying columns row by row and backtracking on conflict.
avg O(n!)space O(n)
Tower of Hanoi
The classic recursion puzzle: move a stack of disks to another peg, never placing a larger disk on a smaller one. Solves in 2ⁿ − 1 moves.
avg O(2ⁿ)space O(n)
Sudoku Solver
Backtracking at its most recognisable: try a digit, recurse, and erase it the moment the branch dies.
avg O(9^m)space O(m)
Two Pointers
On a sorted array, two pointers start at the ends and move inward — growing or shrinking the sum — to find a pair with a target sum in O(n).
avg O(n)space O(1)
Sliding Window
Maintains the sum of a fixed-size window as it slides across the array — adding the entering element and dropping the leaving one — to find the maximum window sum in O(n).
avg O(n)space O(1)
Kadane's Algorithm
The largest sum of any contiguous subarray, in one pass: whenever the running sum goes negative, throw it away and start again.
avg O(n)space O(1)
Boyer–Moore Majority Vote
Finds the value occupying more than half the array with a single counter and no extra memory — different values simply cancel out.
avg O(n)space O(1)
Recursion
A function that calls itself on smaller inputs until a base case. Fibonacci shows the branching call tree grow and unwind — and why naive recursion repeats work.
avg O(φⁿ)space O(n)
Divide & Conquer
Splits a problem into halves, solves each recursively, then combines their answers. Here it finds an array's maximum — the same pattern that powers Merge Sort and Quick Sort.
avg O(n)space O(log n)
Memoization
Caches each recursive result the first time it is computed, so repeated subproblems return instantly. The same fib tree collapses from exponential to linear.
avg O(n)space O(n)
Branch & Bound
Explores the 0/1 knapsack decision tree, but computes an optimistic bound at each node and prunes any branch that cannot beat the best solution found so far.
avg O(2ⁿ)space O(n)
Greedy (Coin Change)
Makes an amount by repeatedly taking the largest coin that fits. Fast and optimal for canonical coin systems — but the demo also shows a set where greedy fails.
avg O(n log n)space O(1)
Union-Find (DSU)
Tracks a partition of elements into disjoint sets, with near-constant-time union and connectivity queries thanks to union-by-rank and path compression.
avg O(α(n))space O(n)
Heap (Binary Max-Heap)
A complete binary tree kept in an array where every parent outranks its children. Insert sifts up and extract-max sifts down — both in O(log n).
avg O(log n)space O(n)
Trie (Prefix Tree)
A tree keyed by characters where words sharing a prefix share a path. Insert and lookup both run in O(L), the length of the word — independent of how many words are stored.
avg O(L)space O(N·L)
Segment Tree
A binary tree of interval aggregates (here, sums) that answers any range query in O(log n) by combining a handful of covering nodes instead of scanning the array.