liveBook · Manning

Constant time

As explained in Fabulous Adventures in Data Structures and Algorithms

Constant-time complexity is computational complexity in which an operation takes a bounded amount of time independent of input size.

An operation is constant time, or O(1), when its time is bounded independently of input size. This describes an individual operation, not necessarily an entire algorithm or sequence of operations. section 2.4

Immutable queues show why worst-case and amortized costs matter. Enqueue, Peek, and IsEmpty take constant time, but Dequeue can take O(n) when it reverses the enqueue stack. Across a sequence of operations, however, each item is moved only a bounded number of times, making dequeue amortized O(1). section 2.4

Constant-time construction, linear-time result

A Hughes list can represent reversing a stack by constructing a delegate in O(1) time. Converting that representation back to a stack takes O(n), so constant-time construction does not make the full reversal constant time. section 2.7.7

The same distinction appears in permutation generation: Fisher–Yates uses O(1) swaps, avoiding O(n) RemoveAt operations. The swap is constant time; the full algorithm performs multiple swaps. section 6.2.3

public static HList<T> Reverse(IImStack<T> fromStack) =>
    fromStack.IsEmpty ?
        Empty :
        Make(stack => fromStack.ReverseOnto(stack));

The Reverse factory represents reversal with a Hughes list; materializing the stack remains linear time. section 2.7.7

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines O(1) and contrasts worst-case, amortized, and deferred costs, without deriving formal bounds.
Examples
Uses queue operations, Hughes-list reversal, and Fisher–Yates swaps to contrast constant and linear costs.
What you can do after
Evaluate operation costs by distinguishing worst-case bounds, amortized bounds, and deferred work.
How it connects
Connects constant time with immutable queues, amortized analysis, Hughes lists, and Fisher–Yates shuffling.

Also explained in

Compare how 7 books explain constant time →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.