ALGORITHM AND PYTHON REFERENCE
Glossary and library choices
Search short definitions and compare Python tools with the algorithm idea they support. These reference lookups do not change course progress.
343 reference entries
Python library comparison
| Tool | Useful for | Behavior and tradeoff | Learn more |
|---|---|---|---|
bisect_leftPython bisect documentation (official docs, new tab) | Find the first valid insertion point in an already sorted sequence. | Binary search takes O(log n) time; values equal to the target stay to the right of the returned position. | Binary search |
insort_leftPython bisect documentation (official docs, new tab) | Insert a value into a sorted Python list while keeping it sorted. | The position search is O(log n), but list insertion can move later values, so the complete operation is O(n). | Binary search |
collections.dequePython collections documentation (official docs, new tab) | Add or remove items at both ends, such as a FIFO queue or a sliding window. | Endpoint appends and pops are approximately O(1); a deque is not designed for fast access to arbitrary middle positions. | Queue and deque operations |
heapqPython heapq documentation (official docs, new tab) | Repeatedly retrieve the smallest pending value from a priority queue. | The list keeps a min-heap ordering; the smallest value is at index 0, while the remaining values are not fully sorted. | Heap operations |
functools.lru_cachePython functools documentation (official docs, new tab) | Reuse results from repeated calls to a function with the same arguments. | Stores recent function results up to a configurable limit; arguments must be hashable. | Dynamic programming |
Glossary
- Algorithm invariant
- A statement that remains true before and after each step; it helps show why an algorithm is correct. In Binary search
- Lower bound
- The first position whose value is not less than a target; the target may be absent. In Binary search
- Upper bound
- The first position whose value is greater than a target; subtracting the lower bound gives the duplicate count. In Binary search
- Stable sort
- A sort that preserves the original relative order of items with equal keys. In Comparison sorts
- Hash collision
- Two distinct keys map to the same hash-table location and need an additional resolution step. In Hash chaining
- Heap invariant
- Each parent has priority over its children; this exposes the next minimum or maximum without fully sorting all values. In Heap operations
- Path compression
- A disjoint-set optimization that makes nodes point closer to their representative during a find operation. In Union-find
- Topological order
- An ordering of a directed acyclic graph in which every edge points from an earlier item to a later item. In Topological order
- Dynamic programming
- A way to solve problems by storing results for overlapping subproblems instead of recomputing them. In Dynamic programming
- Greedy choice
- A locally preferred step; a correctness argument must show why choosing it still permits a globally optimal result. In Greedy methods
- Backtracking
- Explore a choice, continue while it can lead to a solution, and undo it when that path cannot succeed. In Backtracking
- Prefix sum
- A running total that can turn a range-sum query into a subtraction of two prefix values. In Prefix sums
- False positive
- An approximate structure reports that an item may be present even though it is absent; a false negative may be ruled out by its guarantee. In Approximate membership
Library costs
325 cost rows
| Function | Time | Memory | Lesson |
|---|---|---|---|
BloomFilter.add | O(k) | not stated | Approximate membership structures |
BloomFilter.maybe_contains | O(k) | not stated | Approximate membership structures |
PersistentStackFrame.pop | O(1) | not stated | Approximate membership structures |
PersistentStackFrame.push | O(1) | not stated | Approximate membership structures |
SkipList.contains | O(log n) | O(1) extra memory. | Approximate membership structures |
SkipList.insert | O(log n) | O(1) extra memory besides the new node. | Approximate membership structures |
backtrack_find | Depends on ``combine``; worst case explores the full subset tree | not stated | Backtracking search |
combinations | C(n | not stated | Backtracking search |
n_queens_count | Exponential in ``board_size`` in the worst case; pruning does not change the worst-case tree size in theory | not stated | Backtracking search |
permutations | O(n · n!) | not stated | Backtracking search |
subsets | O(n) | not stated | Backtracking search |
bound_vocabulary | O(1) | The dictionary is fixed size: O(1) time and O(1) extra memory. | Input size and operation counts |
classify_recurrence | O(1) | A constant-size table lookup: O(1) time and O(1) extra memory. | Input size and operation counts |
growth_table | Θ(1) | Extra memory is Θ(1) per returned row. | Input size and operation counts |
pair_comparison_count | Θ(n²) | Extra memory is Θ(1) for the indexes. | Input size and operation counts |
recursion_limit | O(1) | not stated | Input size and operation counts |
recursion_tree_levels | Θ(log n) | Extra memory is one row per level. | Input size and operation counts |
UserContext.__init__ | O(1) | Time and extra memory are both O(1). | Appendix: Python tools |
UserContext.require | O(1) | not stated | Appendix: Python tools |
main | O(1) | not stated | Appendix: Python tools |
manage_users | O(1) | not stated | Appendix: Python tools |
modify_database | O(1) | not stated | Appendix: Python tools |
read_database | O(1) | not stated | Appendix: Python tools |
run_action | O(1) | not stated | Appendix: Python tools |
main | O(k) | not stated | Appendix: Python tools |
require_confirmation | O(1) | not stated | Appendix: Python tools |
show_arguments | O(k) | not stated | Appendix: Python tools |
LinkedDeque.__contains__ | O(n) | O(n) time, O(1) extra memory. | Queue and deque operations |
LinkedDeque.__init__ | O(1) | not stated | Queue and deque operations |
LinkedDeque.__iter__ | O(n) | O(n) time, O(1) extra memory. | Queue and deque operations |
LinkedDeque.__len__ | O(1) | not stated | Queue and deque operations |
LinkedDeque.__repr__ | O(n) | Formatting n items is O(n) time and O(n) extra memory. | Queue and deque operations |
LinkedDeque.append_left | O(1) | not stated | Queue and deque operations |
LinkedDeque.append_right | O(1) | not stated | Queue and deque operations |
LinkedDeque.insert_at | O(n) | not stated | Queue and deque operations |
LinkedDeque.pop_left | O(1) | not stated | Queue and deque operations |
LinkedDeque.pop_right | O(1) | not stated | Queue and deque operations |
DoublyLinkedList.__contains__ | O(n) | O(n) time, O(1) extra memory. | Doubly linked-list operations |
DoublyLinkedList.__init__ | O(k) | not stated | Doubly linked-list operations |
DoublyLinkedList.__iter__ | O(n) | Only the current node is kept: O(1) extra memory. | Doubly linked-list operations |
DoublyLinkedList.__len__ | O(1) | not stated | Doubly linked-list operations |
DoublyLinkedList.__repr__ | O(n) | Time and extra memory are both O(n). | Doubly linked-list operations |
DoublyLinkedList.append | O(1) | not stated | Doubly linked-list operations |
DoublyLinkedList.head | O(1) | The head is stored directly: O(1) time, no extra memory. | Doubly linked-list operations |
DoublyLinkedList.insert_after | O(1) | not stated | Doubly linked-list operations |
DoublyLinkedList.move_to_front | O(1) | Total O(1) time and O(1) extra memory. | Doubly linked-list operations |
DoublyLinkedList.pop_tail | O(1) | Extra memory is O(1). | Doubly linked-list operations |
DoublyLinkedList.prepend | O(1) | not stated | Doubly linked-list operations |
DoublyLinkedList.remove_node | O(1) | not stated | Doubly linked-list operations |
DoublyLinkedList.reverse | O(1) | Only ``current`` is stored: O(1) extra memory. | Doubly linked-list operations |
DoublyLinkedList.tail | O(1) | The tail is stored directly: O(1) time, no extra memory. | Doubly linked-list operations |
DoublyNode.__repr__ | O(1) | Neither neighbour is visited, so the cost does not grow with n: O(1) time and O(1) extra memory. | Doubly linked-list operations |
dag_dp_longest_path | O(V + E) | not stated | Dynamic programming |
edit_distance | O(nm) | O(nm) time, O(min(n, m)) memory. | Dynamic programming |
fibonacci_modulo | O(n) | O(1) extra memory for the two running values. | Dynamic programming |
knapsack_01 | O(nW) | O(nW) time and O(W) memory with one rolling row where n = len(weights). | Dynamic programming |
lis_length_patience | O(n log n) | O(n log n) time from binary search on pile tops, O(n) memory. | Dynamic programming |
lis_length_quadratic | O(n²) | O(n²) time, O(n) memory. | Dynamic programming |
longest_common_subsequence | O(nm) | O(nm) time and O(min(n, m)) memory with one row. | Dynamic programming |
max_non_adjacent_sum | O(n) | O(n) time, O(1) memory (two rolling totals). | Dynamic programming |
min_coin_change | O(amount × len(coins)) | O(amount × len(coins)) time, O(amount) memory. | Dynamic programming |
unbounded_knapsack_max_value | O(nW) | O(nW) time, O(W) memory — same table shape as bounded coin change. | Dynamic programming |
word_break_possible | O(D) | Building the prefix trie takes O(D) expected time and O(D) memory. | Dynamic programming |
Graph.__init__ | O(1) | not stated | Graph representations |
Graph.add_edge | O(1) | not stated | Graph representations |
Graph.add_vertex | O(1) | not stated | Graph representations |
Graph.neighbors | O(1) | not stated | Graph representations |
Graph.vertices | O(V) | not stated | Graph representations |
breadth_first_order | O(V + E) | The queue holds at most V labels: O(V) extra memory. | Graph representations |
depth_first_order | O(V + E) | The stack and visited set hold at most V entries: O(V) extra memory. | Graph representations |
dijkstra_distances | O(V^2 + E) | The ``best`` map stores one integer per reachable vertex: O(V) extra memory. | Graph representations |
UndirectedGraph.add_edge | O(1) | not stated | Graph traversals |
bellman_ford_distances | O(VE) | O(V) extra memory. | Graph traversals |
bfs_distances_and_parents | O(V + E) | O(V + E) time, O(V) extra memory for maps and the queue. | Graph traversals |
connected_components | O(V + E) | O(V + E) time, O(V) extra memory. | Graph traversals |
dag_shortest_paths | O(V + E) | not stated | Graph traversals |
dijkstra_distances_heap | O((V + E) log V) | O(V) extra memory for the heap and the distance map. | Graph traversals |
floyd_warshall | O(V³) | O(V³) time, O(V²) memory. | Graph traversals |
has_directed_cycle | O(V + E) | not stated | Graph traversals |
is_bipartite | O(V + E) | not stated | Graph traversals |
kosaraju_strongly_connected_components | O(V + E) | O(V + E) time, O(V) extra memory. | Graph traversals |
kruskal_minimum_spanning_weight | O(E log E) | not stated | Graph traversals |
prim_minimum_spanning_weight | O((V + E) log V) | not stated | Graph traversals |
topological_order | O(V + E) | O(V + E) time and O(V) memory for indegrees and the queue. | Graph traversals |
zero_one_bfs_distances | O(V + E) | O(V + E) time, O(V) memory. | Graph traversals |
fractional_knapsack_value | O(n log n) | not stated | Greedy methods |
huffman_codes | O(n log n) | not stated | Greedy methods |
interval_scheduling_max_count | O(n log n) | O(n log n) sort, O(n) scan, O(1) extra memory besides output count. | Greedy methods |
ChainingHashTable.__delitem__ | O(1) | not stated | Hash tables with chaining |
ChainingHashTable.__getitem__ | O(1) | not stated | Hash tables with chaining |
ChainingHashTable.__init__ | O(bucket_count) | Allocate ``bucket_count`` empty lists: O(bucket_count) time and memory. | Hash tables with chaining |
ChainingHashTable.__iter__ | O(n + b) | Extra memory is O(1) besides the iterator. | Hash tables with chaining |
ChainingHashTable.__len__ | O(1) | not stated | Hash tables with chaining |
ChainingHashTable.__repr__ | O(n) | Copying n pairs is O(n) time and O(n) extra memory. | Hash tables with chaining |
ChainingHashTable.__setitem__ | O(1) | not stated | Hash tables with chaining |
LinearProbingHashTable.__delitem__ | O(1) | not stated | Hash tables with linear probing |
LinearProbingHashTable.__getitem__ | O(1) | not stated | Hash tables with linear probing |
LinearProbingHashTable.__init__ | O(capacity) | O(capacity) time and memory for the slot arrays. | Hash tables with linear probing |
LinearProbingHashTable.__iter__ | O(capacity) | not stated | Hash tables with linear probing |
LinearProbingHashTable.__len__ | O(1) | not stated | Hash tables with linear probing |
LinearProbingHashTable.__repr__ | O(n) | Copying n pairs is O(n) time and O(n) extra memory. | Hash tables with linear probing |
LinearProbingHashTable.__setitem__ | Same probing cost as lookup | not stated | Hash tables with linear probing |
LinearProbingHashTable.load_factor | O(1) | not stated | Hash tables with linear probing |
degenerate_chain_length | O(1) | not stated | Hash tables with linear probing |
heapify | O(n) | No extra list is allocated: O(1) extra memory. | Heap operations |
heapsort | O(n) | The heap reuses the input list: O(1) extra memory besides the array being sorted. | Heap operations |
PriorityQueue.__contains__ | O(n) | O(n) time, O(1) extra memory. | Priority queues |
PriorityQueue.__init__ | O(1) | O(1) time and memory. | Priority queues |
PriorityQueue.__iter__ | O(n) | O(n) time, O(1) extra memory. | Priority queues |
PriorityQueue.__len__ | O(1) | not stated | Priority queues |
PriorityQueue.__repr__ | O(n) | Formatting n items is O(n) time and O(n) extra memory. | Priority queues |
PriorityQueue.decrease_key | O(1) | not stated | Priority queues |
PriorityQueue.peek | O(1) | not stated | Priority queues |
PriorityQueue.pop | O(log n) | not stated | Priority queues |
PriorityQueue.push | O(log n) | not stated | Priority queues |
bfs_layer_sizes | O(V + E) | The deque holds at most V labels: O(V) extra memory. | Cost models and assumptions |
insert_into_sorted | O(n) | O(1) extra memory. | Cost models and assumptions |
list_pop_front_cost_demo | O(1) | not stated | Cost models and assumptions |
timsort_sorted | O(n log n) | The copy uses O(n) extra memory. | Cost models and assumptions |
top_k_smallest | O(n log k) | Building the heap is O(n log k) time and O(k) extra memory. | Cost models and assumptions |
vertex_cover_two_approximation | O(V + E) | The returned cover is O(V); ``vertices`` also creates an O(V) temporary list, so peak extra memory is O(V). | Approximation guarantees |
DynamicArray.__contains__ | O(n) | O(n) time, O(1) extra memory. | Dynamic-array growth |
DynamicArray.__getitem__ | O(1) | not stated | Dynamic-array growth |
DynamicArray.__init__ | O(initial_capacity) | Allocate one list: O(initial_capacity) time and memory. | Dynamic-array growth |
DynamicArray.__iter__ | O(n) | n yields: O(n) time, O(1) extra memory. | Dynamic-array growth |
DynamicArray.__len__ | O(1) | not stated | Dynamic-array growth |
DynamicArray.__repr__ | O(n) | Formatting n items is O(n) time and O(n) extra memory. | Dynamic-array growth |
DynamicArray.append | O(1) | Extra memory is O(n) for n stored items. | Dynamic-array growth |
DynamicArray.pop | O(1) | not stated | Dynamic-array growth |
DynamicArray.total_copy_cost_for_appends | O(append_count) | O(append_count) arithmetic steps, one per append: O(1) extra memory. | Dynamic-array growth |
LinkedList.__contains__ | O(n) | O(n) time, O(1) extra memory. | Singly linked list |
LinkedList.__eq__ | O(n + m) | Building the two temporary lists uses O(n + m) extra memory. | Singly linked list |
LinkedList.__init__ | O(1) | not stated | Singly linked list |
LinkedList.__iter__ | O(n) | The iterator holds one node reference: O(1) extra memory, plus the set inside ``iter_nodes``. | Singly linked list |
LinkedList.__len__ | O(1) | not stated | Singly linked list |
LinkedList.__repr__ | O(n) | Time and extra memory are both O(n). | Singly linked list |
LinkedList.__str__ | O(n) | That is one pass: O(n) time and O(n) extra memory for the result. | Singly linked list |
LinkedList.append | O(1) | No walk: O(1) time and O(1) extra memory for the new node, which is part of the list afterwards. | Singly linked list |
LinkedList.display | O(n) | Total time O(n), extra memory O(n) for the string. | Singly linked list |
LinkedList.extend | O(k) | Extra memory is the k new nodes, which become part of the list. | Singly linked list |
LinkedList.insert | O(n) | Extra memory is the new node: O(1). | Singly linked list |
LinkedList.insert_sorted | O(n) | Extra memory is the new node: O(1). | Singly linked list |
LinkedList.iter_nodes | O(n) | The ``seen`` set stores n ids, so extra memory is O(n). | Singly linked list |
LinkedList.make_circular | O(1) | The tail is cached, so pointing it at the head is one assignment: O(1) time and O(1) extra memory. | Singly linked list |
LinkedList.node_at | O(i) | Extra memory is the one ``node`` reference: O(1). | Singly linked list |
LinkedList.pop | O(1) | Extra memory is O(1). | Singly linked list |
LinkedList.prepend | O(1) | That is a constant number of writes, independent of n: O(1) time and O(1) extra memory. | Singly linked list |
LinkedList.refresh | O(n) | The ``seen`` set holds n ids: O(n) extra memory. | Singly linked list |
LinkedList.remove | O(n) | Extra memory is the ``previous`` reference: O(1). | Singly linked list |
LinkedList.require_linear | O(n) | ``refresh`` is O(n) time and builds a ``seen`` set of n node ids while it runs, so peak extra memory is O(n). | Singly linked list |
LinkedList.tail | O(1) | not stated | Singly linked list |
Node.__repr__ | O(1) | not stated | Singly linked list |
add_numbers | O(n + m) | A few references and the carry are O(1) scratch memory. | Special linked-list cases |
find_cycle_start | O(n) | Only a few references are stored: O(1) extra memory. | Fast and slow pointers |
delete_without_predecessor | O(1) | Copying its value and skipping it is a constant number of assignments: O(1) time, O(1) extra memory. | Special linked-list cases |
remove_sorted_duplicates | O(n) | No auxiliary collection: O(1) extra memory. | Special linked-list cases |
remove_unsorted_duplicates | O(n) | The set stores one entry per distinct value, at most n, so extra memory is O(n). | Special linked-list cases |
find_intersection | O(m) | ``require_linear`` on each list is O(n) + O(m) time with peak O(max(n, m)) extra memory for the guards. | Linked-list merge and sort |
LRUCache.__init__ | O(1) | not stated | LRU cache |
LRUCache.__len__ | O(1) | not stated | LRU cache |
LRUCache.get | O(1) | Total O(1) expected time, O(1) extra memory. | LRU cache |
LRUCache.put | O(1) | Extra memory is O(1) per call. | LRU cache |
merge_sorted | O(n + m) | The result list object is O(1) extra memory. | Linked-list merge and sort |
merge_sorted_into | O(n + m) | Extra memory is a few references: O(1). | Linked-list merge and sort |
end_of_first_half | O(n) | Time O(n), extra memory O(1). | Fast and slow pointers |
middle_node | O(n) | Three references are stored: O(1) extra memory for the walk. | Fast and slow pointers |
modular_node_from_end | O(n) | ``require_linear`` is O(n) time with peak O(n) extra memory for its ``seen`` set. | Fast and slow pointers |
modular_node_from_start | O(n) | One tuple is kept: O(1) extra memory beyond the iterator's ``seen`` set, which is O(n). | Fast and slow pointers |
nth_from_end | O(L) | Extra memory is the two references: O(1). | Fast and slow pointers |
remove_nth_from_end | O(L) | The anchor is one extra node: O(1) extra memory for the walk. | Fast and slow pointers |
group_by_position_parity | O(n) | Only ``odd``, ``even``, and ``even_head`` are stored: O(1) extra memory. | Linked-list reversal and relinking |
is_palindrome | O(n) | The comparison stores a few node references: O(1) extra memory. | Fast and slow pointers |
partition | O(1) | The four head/tail references are O(1) extra memory. | Linked-list reversal and relinking |
RandomNode.__repr__ | O(1) | Time O(1), extra memory O(1). | Doubly linked list and the LRU cache |
clone_interleaved | O(n) | Scratch memory is a few references: O(1), besides the n cloned nodes that form the result. | Doubly linked list and the LRU cache |
clone_with_map | O(n) | The dictionary stores n entries: O(n) extra memory, on top of the n cloned nodes which are the output. | Doubly linked list and the LRU cache |
remove_cycle | O(n) | Extra memory is O(1) on top of whatever ``find_cycle_start`` uses, which is also O(1). | Fast and slow pointers |
reorder | O(n) | Extra memory is a handful of references: O(1). | Linked-list reversal and relinking |
reverse_iterative | O(1) | Three references are stored: O(1) extra memory for the reversal loop. | Linked-list reversal and relinking |
reverse_recursive | O(n) | The calls are nested n deep before any of them returns, so the call stack is O(n) extra memory. | Linked-list reversal and relinking |
reverse_in_blocks | O(n) | The anchor and a few references are O(1) extra memory. | Linked-list reversal and relinking |
reverse_in_pairs | O(n) | The anchor is one extra node: O(1) extra memory. | Linked-list reversal and relinking |
Reviewer.__repr__ | O(1) | not stated | Special linked-list cases |
ReviewersList.__contains__ | O(n) | O(n) time, O(1) extra memory. | Special linked-list cases |
ReviewersList.__init__ | O(1) | Three empty references: O(1) time and O(1) extra memory. | Special linked-list cases |
ReviewersList.__iter__ | O(n) | n reviewers take n steps: O(n) time, O(1) extra memory. | Special linked-list cases |
ReviewersList.__len__ | O(1) | not stated | Special linked-list cases |
ReviewersList.__repr__ | O(n) | Formatting n names is O(n) time and O(n) extra memory. | Special linked-list cases |
ReviewersList.add_reviewer | O(1) | not stated | Special linked-list cases |
ReviewersList.current | O(1) | not stated | Special linked-list cases |
ReviewersList.current_name | O(1) | not stated | Special linked-list cases |
ReviewersList.next_reviewer | O(1) | O(1) time, O(1) extra memory, regardless of how many reviewers are in the circle. | Special linked-list cases |
rotate_right | O(n) | No new node is allocated for the values: O(1) extra memory. | Linked-list reversal and relinking |
sort_list | O(n) | The anchor and a few references are O(1) extra memory for the merge itself. | Linked-list merge and sort |
split_circular | O(n) | The second list object is O(1) extra memory; its nodes are the original ones. | Special linked-list cases |
PrefixSum.__init__ | O(n) | O(n) time and memory. | Two-pointer patterns |
PrefixSum.range_sum | O(1) | not stated | Two-pointer patterns |
longest_unique_substring_length | O(n) | not stated | Two-pointer patterns |
sliding_window_maximum | O(n) | not stated | Two-pointer patterns |
two_sum_sorted | O(n) | O(n) time, O(1) memory. | Two-pointer patterns |
CircularQueue.__contains__ | O(n) | O(n) time, O(1) extra memory. | Queue and deque operations |
CircularQueue.__init__ | O(capacity) | Allocate a list of ``capacity`` slots: O(capacity) time and memory. | Queue and deque operations |
CircularQueue.__iter__ | O(n) | O(n) time, O(1) extra memory. | Queue and deque operations |
CircularQueue.__len__ | O(1) | not stated | Queue and deque operations |
CircularQueue.__repr__ | O(n) | Formatting n items is O(n) time and O(n) extra memory. | Queue and deque operations |
CircularQueue.dequeue | O(1) | not stated | Queue and deque operations |
CircularQueue.enqueue | O(1) | not stated | Queue and deque operations |
CircularQueue.is_empty | O(1) | not stated | Queue and deque operations |
CircularQueue.is_full | O(1) | not stated | Queue and deque operations |
LinkedQueue.__contains__ | O(n) | O(n) time, O(1) extra memory. | Queue and deque operations |
LinkedQueue.__init__ | O(1) | One empty doubly linked list: O(1) time and O(1) extra memory. | Queue and deque operations |
LinkedQueue.__iter__ | O(n) | O(n) time, O(1) extra memory. | Queue and deque operations |
LinkedQueue.__len__ | O(1) | not stated | Queue and deque operations |
LinkedQueue.__repr__ | O(n) | Formatting n items is O(n) time and O(n) extra memory. | Queue and deque operations |
LinkedQueue.dequeue | O(1) | ``remove_node`` on the head unlinks through ``head.next`` and ``head.prev``: O(1) time, O(1) extra memory. | Queue and deque operations |
LinkedQueue.enqueue | O(1) | not stated | Queue and deque operations |
LinkedQueue.is_empty | O(1) | not stated | Queue and deque operations |
LinkedQueue.peek | O(1) | not stated | Queue and deque operations |
FenwickTree.__init__ | O(size) | O(size) memory. | Range-query structures |
FenwickTree.add | O(log n) | not stated | Range-query structures |
FenwickTree.prefix_sum | O(log n) | not stated | Range-query structures |
SegmentTree.__init__ | O(n) | not stated | Range-query structures |
SegmentTree.point_query | O(log n) | not stated | Range-query structures |
SegmentTree.range_add | O(log n) | not stated | Range-query structures |
SparseTable.__init__ | O(n log n) | O(n log n) time and memory. | Range-query structures |
SparseTable.range_min | O(1) | not stated | Range-query structures |
binary_search | O(log n) | A few integer indexes are stored: O(1) extra memory. | Binary search |
binary_search_trace | O(log n) | O(log n) extra memory for the frame list. | Binary search |
lower_bound | O(log n) | Same halving argument as ``binary_search``: O(log n) time, O(1) extra memory. | Binary search |
upper_bound | O(log n) | Still O(log n) time and O(1) extra memory. | Binary search |
bubble_sort | O(n²) | A few index variables are the only extra memory: O(1). | Comparison sorts |
insertion_sort | O(n²) | The hole index and the key are O(1) extra memory. | Comparison sorts |
merge_sort | O(n log n) | Extra memory is O(n) for the halves, plus O(log n) for the call stack. | Merge sort |
quick_sort | O(n log n) | Extra memory is the recursion stack: expected O(log n), worst case O(n). | Quicksort and partition |
quick_sort_three_way | O(n) | Extra memory is the recursion stack: expected O(log n), worst O(n). | Quicksort and partition |
binary_search_on_answer | O(log(high - low)) | not stated | Binary search on a feasible answer |
comparison_sort_lower_bound | O(n) | not stated | Binary search on a feasible answer |
counting_sort | O(n + maximum) | Count array length ``maximum + 1``: O(maximum) extra memory. | Binary search on a feasible answer |
inversion_count | O(n log n) | O(n log n) time, O(n) extra memory for the merge buffer. | Binary search on a feasible answer |
lsd_radix_sort | O(d · (n + base)) | Total O(d · (n + base)) time and O(n + base) extra memory per pass. | Binary search on a feasible answer |
lsd_radix_sort_strings | O(d · (n + 256)) | not stated | Binary search on a feasible answer |
quickselect | O(n) | O(1) extra memory besides the input list indices. | Binary search on a feasible answer |
selection_sort | O(n²) | A handful of indexes is O(1) extra memory. | Comparison sorts |
evaluate_infix | O(n) | The postfix list and the value stack are O(n) extra memory. | Stack parsing and expressions |
evaluate_postfix | O(t) | The value stack holds at most t integers: O(t) extra memory. | Stack parsing and expressions |
infix_to_postfix | O(n) | The operator stack and the output hold at most t tokens: O(n) extra memory. | Stack parsing and expressions |
largest_rectangle | O(n) | The stack holds at most n indexes plus the sentinel: O(n) extra memory. | Monotonic stack patterns |
MinStack.__contains__ | O(n) | O(n) time, O(1) extra memory. | Stack parsing and expressions |
MinStack.__init__ | O(1) | Two empty stacks are allocated: O(1) time and O(1) extra memory. | Stack parsing and expressions |
MinStack.__iter__ | O(n) | O(n) time, O(1) extra memory. | Stack parsing and expressions |
MinStack.__len__ | O(1) | not stated | Stack parsing and expressions |
MinStack.__repr__ | O(n) | Formatting n items is O(n) time and O(n) extra memory. | Stack parsing and expressions |
MinStack.minimum | O(1) | not stated | Stack parsing and expressions |
MinStack.peek | O(1) | not stated | Stack parsing and expressions |
MinStack.pop | O(1) | Extra memory is O(1). | Stack parsing and expressions |
MinStack.push | O(1) | Extra memory grows by one slot per push on the main stack and, for a non-increasing sequence, one slot per push on the minima stack as well. | Stack parsing and expressions |
next_greater | O(n) | The The stack holds at most n indexes: O(n) extra memory. | Monotonic stack patterns |
sort_stack | O(n²) | The extra stack holds every item by the end of the first loop, so extra memory is O(n). | Stack parsing and expressions |
Stack.__contains__ | O(n) | O(n) time, O(1) extra memory. | Stack basics |
Stack.__init__ | O(1) | Allocating an empty list and storing ``limit`` is O(1) time and O(1) extra memory. | Stack basics |
Stack.__iter__ | O(n) | O(n) time, O(1) extra memory. | Stack basics |
Stack.__len__ | O(1) | not stated | Stack basics |
Stack.__repr__ | O(n) | Time and extra memory are O(n). | Stack basics |
Stack.is_empty | O(1) | not stated | Stack basics |
Stack.is_full | O(1) | not stated | Stack basics |
Stack.peek | O(1) | O(1) time, O(1) extra memory. | Stack basics |
Stack.pop | O(1) | Extra memory is O(1). | Stack basics |
Stack.push | O(1) | not stated | Stack basics |
stock_spans | O(n) | The stack holds at most n indexes: O(n) extra memory besides the result list. | Monotonic stack patterns |
symbols_are_balanced | O(1) | The stack holds only unmatched openers, at most n of them, so extra memory is O(n) in the worst case (a string of only openers) and O(1) when brackets stay matched along the way. | Stack parsing and expressions |
BTree.insert | O(log n) | O(1) extra memory besides the new node. | Storage engines and locality |
BTree.search | O(log n) | O(1) extra memory besides the call stack, which is O(log n). | Storage engines and locality |
ToyLSM.maybe_contains | O(log m + r) | O(1) extra memory. | Storage engines and locality |
ToyLSM.put | O(log m) | not stated | Storage engines and locality |
WriteAheadLog.append | O(1) | O(1) extra memory for the new entry. | Storage engines and locality |
WriteAheadLog.replay | O(n log m) | O(1) extra memory besides the tree. | Storage engines and locality |
locality_scan_benchmark | O(repeats · n) | not stated | Storage engines and locality |
nearest_neighbor_linear | O(nd) | not stated | Storage engines and locality |
sequential_scan_sum | O(n) | not stated | Storage engines and locality |
InvertedIndex.add_document | O(total tokens) | not stated | String search algorithms |
InvertedIndex.query | O(length of posting list) | not stated | String search algorithms |
Trie.__init__ | O(1) | not stated | String search algorithms |
Trie.contains | O(len(key)) | not stated | String search algorithms |
Trie.insert | O(len(key)) | not stated | String search algorithms |
kmp_search | O(n + m) | not stated | String search algorithms |
rabin_karp_search | O(n + m) | not stated | String search algorithms |
run_length_encode | O(n) | not stated | String search algorithms |
suffix_array_with_lcp | O(n log² n) | not stated | String search algorithms |
format_tree | O(e) | The returned string is the output, so extra memory is O(e). | Appendix: Python tools |
main | O(1) | not stated | Appendix: Python tools |
BinarySearchTree.__contains__ | O(n) | O(n) time, O(1) extra memory besides the walk. | Binary search tree operations |
BinarySearchTree.__init__ | O(1) | O(1) time and O(1) extra memory. | Binary search tree operations |
BinarySearchTree.__iter__ | O(n) | O(n) time, O(h) extra memory. | Binary search tree operations |
BinarySearchTree.__len__ | O(n) | O(n) time, O(h) extra memory. | Binary search tree operations |
BinarySearchTree.__repr__ | O(n) | Formatting n keys is O(n) time and O(n) extra memory. | Binary search tree operations |
BinarySearchTree.contains | O(h) | Same walk as ``insert`` without creating nodes: O(h) time, O(1) extra memory iteratively. | Binary search tree operations |
BinarySearchTree.delete | O(h) | Total O(h) time, O(1) extra memory iteratively. | Binary search tree operations |
BinarySearchTree.inorder | O(n) | Each node is pushed and popped once on the stack: O(n) time, O(h) extra memory for the stack where h is the height. | Binary search tree operations |
BinarySearchTree.inorder_iter | O(n) | The stack holds at most h nodes: O(h) extra memory. | Binary search tree operations |
BinarySearchTree.insert | O(h) | h levels take h steps: O(h) time and O(1) extra memory. | Binary search tree operations |
LeftLeaningRedBlackTree.__contains__ | O(n) | O(n) time, O(1) extra memory. | Balanced search trees |
LeftLeaningRedBlackTree.__init__ | O(1) | not stated | Balanced search trees |
LeftLeaningRedBlackTree.__iter__ | O(n) | O(n) time, O(n) extra memory for the list. | Balanced search trees |
LeftLeaningRedBlackTree.__len__ | O(1) | not stated | Balanced search trees |
LeftLeaningRedBlackTree.__repr__ | O(n) | Formatting n keys is O(n) time and O(n) extra memory. | Balanced search trees |
LeftLeaningRedBlackTree.contains | O(log n) | not stated | Balanced search trees |
LeftLeaningRedBlackTree.delete | O(log n) | not stated | Balanced search trees |
LeftLeaningRedBlackTree.inorder | O(n) | O(n) time, O(n) output memory. | Balanced search trees |
LeftLeaningRedBlackTree.insert | O(log n) | Total O(log n) time, O(log n) call-stack memory recursively (O(1) if rewritten iteratively). | Balanced search trees |
LeftLeaningRedBlackTree.rank | O(log n) | not stated | Balanced search trees |
LeftLeaningRedBlackTree.select | O(log n) | not stated | Balanced search trees |
inorder_iter | O(n) | O(n) time, O(h) extra memory. | Tree traversals |
level_order | O(n) | The queue holds at most one frontier: O(n) extra memory in the worst case (complete last level). | Tree traversals |
map_inorder | O(n) | Same as ``BinarySearchTree.inorder_iter``: O(n) time, O(h) stack memory. | Tree traversals |
postorder | O(n) | Two passes with a stack, still O(n) time and O(h) extra memory. | Tree traversals |
preorder | O(n) | Each node is pushed and popped once on the stack: O(n) time, O(h) extra memory for height h (O(n) if the tree is a chain). | Tree traversals |
DisjointSet.__init__ | O(size) | O(size) time and memory to fill parent (and rank) arrays. | Union-find |
DisjointSet.connected | Two ``find`` calls | not stated | Union-find |
DisjointSet.find | O(n) | Extra memory O(1) besides the arrays. | Union-find |
DisjointSet.union | O(1) | not stated | Union-find |
Percolation.__init__ | O(size²) | O(size²) sites plus one virtual node: O(size²) memory. | Union-find |
Percolation.is_open | O(1) | not stated | Union-find |
Percolation.open | O(α(n²)) | O(1) extra memory per call. | Union-find |
Percolation.open_site_count | O(1) | not stated | Union-find |
Percolation.percolates | O(size · α(size²)) | not stated | Union-find |