Linear search versus binary search
#Linear search works without sorted order. Binary search repeatedly narrows a monotone search interval.
Fragment
On a linked list, reaching each midpoint is not constant-time indexed access.
Reference
Compare operation costs, ordering, invariants and common algorithm notation.
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.
Linear search works without sorted order. Binary search repeatedly narrows a monotone search interval.
Fragment
On a linked list, reaching each midpoint is not constant-time indexed access.
Sorting costs depend on the algorithm, comparator and representation. Stable sorting preserves relative order of equal keys.
Fragment
In-place variants and hybrid library sorts can have different details. Never assume a native sort is stable without its contract.
General comparison sorting needs Ω(n log n) comparisons in the worst case. Counting/radix methods use extra assumptions about keys.
Fragment
These methods trade key assumptions and extra storage for a different cost model.
Selection finds the kth ordered element. Quickselect is expected linear under suitable pivot selection but has quadratic worst cases.
Fragment
Duplicate handling and rank indexing must be defined.
A prefix array stores cumulative sums. For half-open ranges [l,r), subtract prefix endpoints.
Fragment
Watch numeric overflow and use a consistent inclusive/exclusive convention.
Both maintain aggregate information for updates and range queries. Supported operations depend on the algebra of the aggregate.
Fragment
Lazy propagation supports selected range updates, not arbitrary update/query combinations.
Memoization/dynamic programming avoids recomputing equivalent states. The cost is governed by the number of reached states and transitions evaluated per state.
Fragment
A state must include everything affecting the answer. A cache keyed too narrowly changes correctness.
Two nested-looking loops can be linear when each pointer moves monotonically across the input only once.
Fragment
The algorithm still needs a valid invariant; a sliding window is not suitable for every predicate or negative-valued input.
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
Use the same comparator/partition relation the algorithm requires. Conventional array searches take O(log n) comparisons.
Stable sorting retains the relative order of equal keys. The following describe conventional implementations, not every library’s hybrid or optimized variant.
Fragment
* With tie handling that preserves order. Quicksort stack can be O(n) in the worst case; recursion strategy and pivot choice affect it.
A substring is contiguous; a subsequence preserves order without requiring adjacency. Prefixes and suffixes touch the corresponding end.
Fragment
For text algorithms, define whether positions count bytes, code units or Unicode scalar values.