Algorithms and Data Structures
An algorithm is a precise, finite sequence of steps that turns an input into a result, like a recipe. A data structure is a way to store data in memory so that certain operations are fast: an array finds an element by position instantly, a hash table finds it by key. Algorithms are compared by how their cost grows as the input grows, written in Big O notation: O(n) means the work grows at most proportionally to the input size n, O(_n_²) — at most as its square, O(1) — stays below a constant whatever n is. Formally Big O is an upper bound on growth, with constant factors ignored; it is usually quoted for the worst case, but it is a bound, not a case.
Details
Measuring Complexity
- Time complexity counts basic steps (comparisons, assignments) as a function of the input size n; space complexity counts the extra memory.
- Formal definition: f(n) = O(g(n)) if there are constants c > 0 and n₀ such that f(n) ≤ c · g(n) for all n ≥ n₀. In words: from some size on, f grows no faster than g up to a constant factor.
- f = Ω(g) if f(n) ≥ c · g(n) for some c > 0 and all n ≥ n₀ (grows at least as fast); f = Θ(g) if it is both O(g) and Ω(g).
- log n in plain words: how many times n can be halved until 1 is left. log₂ 1,000 ≈ 10, log₂ 1,000,000 ≈ 20: doubling the input adds one step.
| Notation | Bound | Reads as |
|---|---|---|
| O(g) | Upper: grows no faster than g | "at most of order g" |
| Ω(g) | Lower: grows no slower than g | "at least of order g" |
| Θ(g) | Tight: both O(g) and Ω(g) | "exactly of order g" |
In everyday speech "O" is often used where Θ is meant: "binary search is O(log n)" means that in the worst case it grows exactly like log n (in the best case it finds the element at once).
Rules for computing Big O:
- Drop constant factors and lower-order terms: 3_n_² + 5_n_ + 2 → O(_n_²).
- Steps in sequence add: O(n) + O(_n_²) = O(_n_²).
- Nested loops multiply: a loop over n inside a loop over m is O(n · m).
- The base of a logarithm does not matter: log₂ n and log₁₀ n differ by a constant factor.
Kinds of cost:
- Best, average and worst case: different inputs of the same size cost differently. Quicksort is Θ(n log n) on average and Θ(_n_²) in the worst case.
- Amortized cost is the worst-case total cost of any sequence of n
operations divided by n: a guarantee, not an average over random inputs.
pushinto a dynamic array is occasionally O(n) (copying into a bigger buffer), but O(1) amortized. - Recursion costs memory too: each pending call takes a stack frame, so a recursion of depth d uses O(d) space.
Complexity Classes
| Notation | Name | Example | n = 1,000 → steps |
|---|---|---|---|
| O(1) | Constant | Array element by index, Map.get (in practice) | 1 |
| O(log n) | Logarithmic | Binary search in a sorted array | ~10 |
| O(n) | Linear | Linear search, one loop over the input | 1,000 |
| O(n log n) | Linearithmic | Merge sort, Array.prototype.sort | ~10,000 |
| O(_n_²) | Quadratic | Bubble sort, two nested loops over the input | 1,000,000 |
| O(2n) | Exponential | All subsets; naive Fibonacci (tightly Θ(1.618ⁿ)) | ~10³⁰¹ |
| O(n!) | Factorial | Generating all permutations | ~10²⁵⁶⁷ |
Abstract Data Types and Data Structures
- An abstract data type (ADT) defines what operations are available and
what they do: a stack has
pushandpop, a map hasgetandset. - A data structure is how it is implemented in memory: a stack can be built on an array or on a linked list.
| ADT | Operations | Typical implementation | In JavaScript |
|---|---|---|---|
| List | Access by position, insert, delete | Dynamic array, linked list | Array |
| Stack (LIFO) | push, pop, peek — last in, first out (undo, the browser's Back, the call stack) | Array | Array with push / pop |
| Queue (FIFO) | enqueue, dequeue — first in, first out (a line at a shop, the event loop's task queue) | Linked list, ring buffer | No built-in; see below |
| Map | get, set, delete by key (a dictionary: word → definition) | Hash table, balanced tree | Map, plain objects |
| Set | add, has, delete, unique values | Hash table | Set |
| Priority queue | Insert, take the min / max | Binary heap | No built-in |
Operations by Structure
Average case unless stated.
| Structure | Access by index | Search | Insert / delete | Notes |
|---|---|---|---|---|
| Array | O(1) | O(n) | O(1) amortized at the end, O(n) elsewhere | Elements after the position shift |
| Sorted array | O(1) | O(log n) | O(n) | Binary search; inserting keeps the order by shifting |
| Linked list | O(n) | O(n) | O(1) at a known node | Finding the node is O(n); deleting needs the previous node (O(1) in a doubly linked list) |
| Hash table | — | O(1) | O(1) | O(n) worst case when all keys collide |
| BST | — | O(log n) | O(log n) | Keeps keys sorted; O(n) in the worst case if unbalanced |
| Binary heap | — | O(n) | Insert O(log n), take min / max O(log n) | Peek at the min / max is O(1) |
Cost of Common JavaScript Operations
| Operation | Cost |
|---|---|
arr[i], arr.length, push, pop | O(1) (push amortized) |
shift, unshift, splice, includes, indexOf, find | O(n) |
slice, spread [...arr], concat, map, filter, reduce | O(n) |
sort, toSorted | O(n log n) |
Map / Set: get, set, has, add, delete | O(1) average |
| Object property read or write | O(1) average |
Object.keys, JSON.stringify, structuredClone | O(size of the object) |
Arrays and Linked Lists
- A classic array is a block of contiguous memory: the address of element i is computed directly, so access is O(1), but inserting in the middle shifts the rest.
- A dynamic array grows by allocating a bigger block and copying. The growth
must be geometric (1.5–2 times larger) for
pushto be O(1) amortized; growing by a fixed amount would make it O(n). - JavaScript arrays are objects; engines store dense arrays contiguously, but
switch to slower dictionary storage for sparse arrays (
arr[1_000_000] = 1on an empty array). - A linked list stores each element in a node with a pointer to the next (singly linked) or to the next and previous (doubly linked). Inserting or removing at a known node is O(1); access by index walks the list.
- In JavaScript,
shift()on an array is O(n): the remaining elements are reindexed. A large queue is better built on a linked list or an array with a head index.
Hash Tables
- A hash function turns a key into a number; the number modulo the table size is the index of a bucket where the value is stored.
- A collision is two keys in the same bucket. It is resolved by chaining (a list per bucket) or open addressing (probe other slots in a fixed sequence until a free one is found).
- When the table fills up (the load factor passes a threshold), it is resized and all entries are rehashed: O(n) once, O(1) amortized.
Mapis a hash table; a plain object behaves like one for dynamic keys (V8 keeps a fixed shape as a hidden class and switches to a dictionary when keys are added and removed). When to use which is in Map and Set.
Trees
- A tree is a connected hierarchy of nodes without cycles: one root, and every other node has exactly one parent. A node without children is a leaf; the height is the longest path from the root to a leaf. Examples: the DOM, a file system, a component tree, an AST (the syntax tree a compiler builds from code).
- A binary tree: each node has at most two children.
- A binary search tree (BST): for every node, the keys in the left subtree are smaller and in the right subtree larger, so search goes down one path — O(height). Balanced trees (AVL, red-black) keep the height O(log n).
- A heap is a complete binary tree where each parent is ≤ its children (min-heap) or ≥ them (max-heap); the minimum is always at the root. It is stored in an array: the children of index i are at 2_i_ + 1 and 2_i_ + 2, the parent at ⌊(i − 1) / 2⌋. Building a heap from n elements is O(n). This data structure is unrelated to the "heap" in memory management.
- A trie (prefix tree) stores strings character by character along paths: autocomplete, search by prefix.
Graphs
- A graph is a set of vertices connected by edges: directed or undirected, weighted or not, with or without cycles. Examples: social networks, routes, links between pages.
- A DAG (directed acyclic graph) has no cycles: module dependencies, task dependencies. Topological sort orders its vertices so that for every edge u → v, u comes before v.
- Representations: an adjacency list (for each vertex, a list of its neighbors; O(V + E) memory, good for sparse graphs) or an adjacency matrix (a V × V table; O(V²) memory, O(1) to check one edge).
- Traversal, both O(V + E) with an adjacency list (O(V²) with a matrix):
- Depth-first search (DFS) goes as deep as possible before backtracking; uses a stack or recursion. Finds cycles, used for topological sort.
- Breadth-first search (BFS) visits vertices level by level; uses a queue. Finds the shortest path in an unweighted graph.
- Shortest paths in a weighted graph with non-negative weights: Dijkstra's algorithm, O((V + E) log V) with a binary heap.
Sorting
| Algorithm | Best | Average | Worst | Extra memory | Stable | Idea |
|---|---|---|---|---|---|---|
| Bubble sort | O(n) | O(_n_²) | O(_n_²) | O(1) | Yes | Swap neighbors until nothing changes |
| Selection sort | O(_n_²) | O(_n_²) | O(_n_²) | O(1) | No | Find the minimum of the rest, put it next |
| Insertion sort | O(n) | O(_n_²) | O(_n_²) | O(1) | Yes | Insert each element into the sorted part; fast on nearly sorted data |
| Merge sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | Split in halves, sort them, merge |
| Quicksort | O(n log n) | O(n log n) | O(_n_²) | O(log n)* | No | Partition around a pivot, sort the parts |
| Heapsort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | Build a heap, take the max repeatedly |
| TimSort | O(n) | O(n log n) | O(n log n) | O(n) | Yes | Find sorted runs, extend short ones with insertion sort, merge runs; V8 since 2018 |
* Quicksort's stack is O(log n) on average, or always when it recurses into the smaller part and loops over the larger one; otherwise O(n) in the worst case.
- A stable sort keeps equal elements in their original order: sort users by name, then stably by age — users of the same age stay sorted by name.
- Any comparison-based sort needs Ω(n log n) comparisons in the worst (and average) case. Non-comparison sorts (counting sort, radix sort) can be O(n) for integer keys from a small range.
Array.prototype.sortis stable since ES2019 and, without a comparator, compares elements as strings:[10, 9, 1].sort()gives[1, 10, 9]. Pass(a, b) => a - bfor numbers. See Arrays.
Common Techniques
| Technique | Idea | Typical task |
|---|---|---|
| Hash map lookup | Trade memory for time: remember what was seen in a Map / Set | Two sum, duplicates, counting |
| Two pointers | Move two indices toward each other or at different speeds | Pairs in a sorted array, palindromes, cycle in a list |
| Sliding window | Keep a window over the array and update it incrementally | Longest substring without repeats |
| Divide and conquer | Split the problem, solve the parts recursively, combine | Merge sort; binary search (decrease and conquer) |
| Dynamic programming | For problems with optimal substructure and overlapping subproblems: solve each subproblem once, reuse the results (memoization or a table) | Fibonacci, edit distance, knapsack |
| Greedy | Take the locally best choice at each step; correct only for some problems | Interval scheduling |
Example
Binary search: O(log n)
function binarySearch(sorted, target) {
let low = 0;
let high = sorted.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (sorted[mid] === target) return mid;
if (sorted[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}
console.log(binarySearch([1, 3, 5, 7, 9, 11], 7)); // 3
console.log(binarySearch([1, 3, 5, 7, 9, 11], 4)); // -1
O(n²) → O(n) with a Set
// O(n²): includes() scans the array for every element
function hasDuplicatesSlow(items) {
return items.some((item, i) => items.slice(i + 1).includes(item));
}
// O(n): Set lookups are O(1) on average
function hasDuplicates(items) {
return new Set(items).size !== items.length;
}
console.log(hasDuplicatesSlow([1, 2, 3, 2]), hasDuplicates([1, 2, 3, 2])); // true true
console.log(hasDuplicates([1, 2, 3])); // false
BFS: shortest path in an unweighted graph
const graph = {
A: ['B', 'C'],
B: ['D'],
C: ['D', 'E'],
D: ['F'],
E: ['F'],
F: [],
};
function shortestPath(graph, start, goal) {
const queue = [start];
const parent = new Map([[start, null]]); // also marks visited vertices
for (let head = 0; head < queue.length; head++) {
const node = queue[head]; // a head index instead of shift(): O(1)
if (node === goal) {
const path = [];
for (let v = goal; v !== null; v = parent.get(v)) path.push(v);
return path.reverse();
}
for (const next of graph[node]) {
if (!parent.has(next)) {
parent.set(next, node);
queue.push(next);
}
}
}
return null;
}
console.log(shortestPath(graph, 'A', 'F')); // [ 'A', 'B', 'D', 'F' ]
Dynamic programming: Fibonacci from O(2ⁿ) to O(n)
// O(2ⁿ): the same values are computed again and again
const fibSlow = (n) => (n < 2 ? n : fibSlow(n - 1) + fibSlow(n - 2));
// O(n): each value is computed once
function fib(n) {
let [prev, curr] = [0, 1];
for (let i = 0; i < n; i++) [prev, curr] = [curr, prev + curr];
return prev;
}
console.log(fibSlow(20), fib(20), fib(50)); // 6765 6765 12586269025
Follow-up Questions
- Why drop constants in Big O? It describes growth: for large n, O(n) beats O(_n_²) whatever the constants. For small inputs, the constants can matter more: insertion sort beats merge sort on a dozen elements.
- O vs Θ? O is an upper bound, Θ is a tight bound. Linear search is also O(_n_²) (an upper bound need not be tight), but Θ(n) in the worst case.
- Why is a hash table lookup O(1) only on average? On average each bucket holds a constant number of keys; if many keys collide, a lookup scans a long bucket, up to O(n).
- What is the complexity of
Array.prototype.sort? O(n log n); V8 uses TimSort, which is stable. - When is a linked list better than an array? When elements are often inserted or removed in the middle at a known position, and access by index is rare. In practice JS arrays win in most cases because of memory locality.
- What is the complexity of
arr.filter(…).map(…).reduce(…)? O(n): passes in sequence add up, they do not multiply. - How to find the k-th largest element? Sorting: O(n log n); a min-heap of size k: O(n log k); quickselect: O(n) on average, O(_n_²) in the worst case.
- How to make a queue with O(1) dequeue in JavaScript? A head index over an array, a linked list or a ring buffer: O(1); two stacks: O(1) amortized.
Common Mistakes
- Calling Big O "the worst case": it is an upper bound and can describe any case.
- Forgetting the hidden loops:
includes,indexOf,shift,spliceinside a loop turn O(n) into O(_n_²). - Saying a linked list inserts in O(1) without adding "at a known node".
- Sorting numbers with
sort()without a comparator.
See Also
- Arrays
- Common Coding Tasks — live-coding tasks in JavaScript: LRU cache, event emitter, deep copy.
- "Introduction to Algorithms" by Cormen, Leiserson, Rivest and Stein — the standard textbook.
- VisuAlgo — animated data structures and algorithms.