Reference

Data structures & costs

Compare operation costs, ordering, invariants and common algorithm notation.

Scope and conventions

Abstract data structures and conventional implementations. Costs use a unit-cost RAM model unless stated; n is item count, V vertices, E edges and k key/output length. Native containers can have different contracts. Expected, amortized and worst-case bounds are distinguished.

Common operations

Linear search versus binary search

Operations & costs

Linear search works without sorted order. Binary search repeatedly narrows a monotone search interval.

Fragment

Operations & costs · Linear search versus binary search
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Linear search             O(n) comparisons
Binary search             O(log n) comparisons
Precondition for binary   sorted/monotone predicate

On a linked list, reaching each midpoint is not constant-time indexed access.

Common sorting guarantees

Operations & costs

Sorting costs depend on the algorithm, comparator and representation. Stable sorting preserves relative order of equal keys.

Fragment

Operations & costs · Common sorting guarantees
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Merge sort                O(n log n), typical O(n) auxiliary
Heap sort                 O(n log n), array form O(1) extra
Randomized quicksort      expected O(n log n), worst O(n²)
Insertion sort            O(n²), useful for small/nearly sorted input

In-place variants and hybrid library sorts can have different details. Never assume a native sort is stable without its contract.

Know when the comparison-sort bound applies

Operations & costs

General comparison sorting needs Ω(n log n) comparisons in the worst case. Counting/radix methods use extra assumptions about keys.

Fragment

Operations & costs · Know when the comparison-sort bound applies
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Counting sort             O(n + k), key range size k
Radix sort                O(d(n + b)), d digits, base b

These methods trade key assumptions and extra storage for a different cost model.

Select one rank without fully sorting

Operations & costs

Selection finds the kth ordered element. Quickselect is expected linear under suitable pivot selection but has quadratic worst cases.

Fragment

Operations & costs · Select one rank without fully sorting
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Quickselect               expected O(n), worst O(n²)
Median-of-medians         worst-case O(n)
Sort then index           O(n log n)

Duplicate handling and rank indexing must be defined.

Precompute range sums

Operations & costs

A prefix array stores cumulative sums. For half-open ranges [l,r), subtract prefix endpoints.

Fragment

Operations & costs · Precompute range sums
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
prefix[0] = 0
prefix[i+1] = prefix[i] + value[i]
sum(l,r) = prefix[r] - prefix[l]
Build O(n); query O(1); simple point update O(n)

Watch numeric overflow and use a consistent inclusive/exclusive convention.

Fenwick and segment trees

Operations & costs

Both maintain aggregate information for updates and range queries. Supported operations depend on the algebra of the aggregate.

Fragment

Operations & costs · Fenwick and segment trees
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Fenwick tree:
  point add / prefix sum  O(log n)
Segment tree:
  point update / range query O(log n)
Storage                   O(n) conventional forms

Lazy propagation supports selected range updates, not arbitrary update/query combinations.

Count states and transitions

Operations & costs

Memoization/dynamic programming avoids recomputing equivalent states. The cost is governed by the number of reached states and transitions evaluated per state.

Fragment

Operations & costs · Count states and transitions
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Time ≈ states × work per state
Space ≈ retained state values + active stack

A state must include everything affecting the answer. A cache keyed too narrowly changes correctness.

Recognize linear total pointer movement

Operations & costs

Two nested-looking loops can be linear when each pointer moves monotonically across the input only once.

Fragment

Operations & costs · Recognize linear total pointer movement
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Each pointer advances at most n times
Total advances ≤ 2n -> O(n)

The algorithm still needs a valid invariant; a sliding window is not suitable for every predicate or negative-valued input.

Distinguish lower and upper bounds

Operations & costs

In ascending sorted order, lower_bound finds the first position not less than the target; upper_bound finds the first position greater than it. A position may equal the end, even when no item equals the target.

Fragment

Operations & costs · Distinguish lower and upper bounds
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
values:             [1, 2, 2, 4]
lower_bound(2):     index 1
upper_bound(2):     index 3
Equal-value range: [1,3)
lower_bound(3):     index 3 (insertion position)
upper_bound(4):     index 4 (end)

Use the same comparator/partition relation the algorithm requires. Conventional array searches take O(log n) comparisons.

Compare conventional sorting properties

Operations & costs

Stable sorting retains the relative order of equal keys. The following describe conventional implementations, not every library’s hybrid or optimized variant.

Fragment

Operations & costs · Compare conventional sorting properties
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Algorithm       Stable?       Typical extra space
Merge sort      yes*          O(n)
Insertion sort  yes*          O(1)
Heap sort       no            O(1)
Quicksort       no            recursion stack

* With tie handling that preserves order. Quicksort stack can be O(n) in the worst case; recursion strategy and pivot choice affect it.

Separate substring, subsequence and prefix

Operations & costs

A substring is contiguous; a subsequence preserves order without requiring adjacency. Prefixes and suffixes touch the corresponding end.

Fragment

Operations & costs · Separate substring, subsequence and prefix
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Text: "abcde"
"bcd" is a substring
"ace" is a subsequence, not a substring
"abc" is a prefix; "cde" is a suffix

For text algorithms, define whether positions count bytes, code units or Unicode scalar values.