36 algorithms · step through every line
See algorithms think.
Watch every comparison, swap and pointer move as an animation you can pause and rewind, with the real source code highlighted line by line as it runs.
Sorting
Put things in order and watch every comparison and swap.
Bubble Sort
O(n²)Repeatedly steps through the list, compares adjacent pairs and swaps them if they are out of order.
Selection Sort
O(n²)Scans the unsorted suffix for its smallest element and swaps it to the front.
Insertion Sort
O(n²)Grows a sorted prefix one element at a time: each new key is shifted left past every larger element until it reaches its place.
Shell Sort
O(n^1.5)An insertion sort over interleaved sub-arrays: elements gap apart are sorted first, then the gap shrinks until it is 1.
Merge Sort
O(n log n)Bottom-up merge sort: runs of width 1, 2, 4, … are merged pairwise into an auxiliary buffer and copied back, doubling the sorted run length each pass.
Quick Sort
O(n log n)Picks the last element as a pivot and partitions the range so smaller elements land to its left (Lomuto scheme); the pivot is then in its final place and both sides are sorted recursively.
Heap Sort
O(n log n)Arranges the array as a binary max-heap, then repeatedly swaps the root (the maximum) to the end and sifts the new root down to restore the heap.
Counting Sort
O(n + k)Never compares elements: it tallies how often each value occurs, turns the tally into prefix sums (each value's end position), then places every element straight into the output.
Radix Sort
O(d·(n + b))Sorts by one decimal digit at a time, least significant first, using a stable counting sort for each digit.
Searching
Find a target by scanning, halving, or jumping ahead.
Linear Search
O(n)Checks every element from left to right until it finds the target or runs out of array.
Binary Search
O(log n)On a sorted array, compares the target with the middle of the remaining range and throws away the half that cannot contain it.
Jump Search
O(√n)On a sorted array, jumps ahead √n elements at a time until it overshoots the target, then scans that one block linearly.
Data Structures
Lists, stacks, queues, hash tables, trees and heaps, op by op.
Dynamic Array
O(n)A contiguous block of slots with a size and a capacity.
Singly Linked List
O(n)Each node stores a value and a pointer to the next node, so inserting or deleting rewires a pointer or two instead of shifting elements.
Doubly Linked List
O(n)Every node links both to its successor and its predecessor, and the list keeps a tail pointer.
Stack
O(1)Last in, first out: push adds on top and pop takes from the top, both in O(1).
Queue
O(1)First in, first out: enqueue joins at the rear and dequeue leaves from the front.
Hash Table
O(1)Maps each key to one of m buckets with hash(k) = k mod m and keeps colliding keys in a chain.
Binary Search Tree
O(log n)Every key in a node's left subtree is smaller and every key in its right subtree is larger, so search, insert and delete each follow one root-to-leaf path.
AVL Tree
O(log n)A self-balancing BST: every node's subtrees differ in height by at most one.
Min-Heap
O(log n)A complete binary tree stored in an array where every parent is ≤ its children, so the minimum is always at the root.
Trie
O(L)A prefix tree: each edge is a letter and each path from the root spells a prefix, so words that share a prefix share nodes.
Graphs
Traversals, shortest paths and spanning trees on grids and networks.
BFS (Grid)
O(V + E)Breadth-first search spreads out from the start one ring at a time using a FIFO queue, so cells are reached in order of their distance in moves.
DFS (Grid)
O(V + E)Depth-first search keeps going in one direction (up first, then right, down and left) and only backtracks to the most recent unexplored cell on its stack when it hits a dead end.
Dijkstra (Grid)
O((V + E) log V)Dijkstra's algorithm always expands the open cell with the smallest known distance, so on a weighted grid it finds the cheapest path rather than the one with fewest moves.
A* Search
O((V + E) log V)A* is Dijkstra's algorithm steered by a heuristic: it expands the cell with the smallest f = g + h, where g is the cost so far and h is the Manhattan distance still to go.
BFS (Graph)
O(V + E)Breadth-first search visits the source, then all of its neighbours, then their unseen neighbours, using a FIFO queue.
DFS (Graph)
O(V + E)Depth-first search follows one branch as deep as it can before backing up to the most recent node that still has an unexplored neighbour.
Dijkstra (Graph)
O(V²)Dijkstra's algorithm grows a set of settled nodes outward from the source, always settling the unfinished node with the smallest tentative distance and then relaxing its edges.
Topological Sort
O(V + E)Kahn's algorithm orders a directed acyclic graph so every edge points forward: it repeatedly outputs a node with no remaining incoming edges and deletes that node's outgoing edges.
Prim's MST
O(V²)Prim's algorithm grows a minimum spanning tree from a single node, each round adding the cheapest edge that connects the tree to a node outside it.
Kruskal's MST
O(E log E)Kruskal's algorithm considers edges from lightest to heaviest and keeps each one that joins two different components, skipping any edge that would close a cycle.
Dynamic Programming
Fill the table one subproblem at a time.
Fibonacci
O(n)Computes F(n) bottom-up: write down the base cases F(0) = 0 and F(1) = 1, then fill each cell as the sum of the two before it.
Longest Common Subsequence
O(m·n)Finds the longest sequence of letters that appears in both strings in the same order, not necessarily side by side.
0/1 Knapsack
O(n·W)Picks the subset of items with the highest total value whose total weight fits the capacity, where each item is either taken whole or left behind.
Edit Distance
O(m·n)Counts the fewest single-letter inserts, deletes and substitutions that turn one string into another (Levenshtein distance).