divide-and-conquer
Merge sort
Merge sort runtime
O(n log n)
- at level j in the *recursive tree*:
-- 2^j sub-problems
-- n /(2^j) items per sub-problem
-- upper bound per sub-problem:
--- 6 * #items
- total lines executed at level j:
2^j * [6 * n / (2^j)] = 6n
- total levels from root to leaf in *recursive tree*:
log_2(n) + 1
- total lines executed in recursive tree:
[log_2(n) + 1] * 6nAsymptotic time complexity
Counting inversions
Quick Sort
Random contraction algo
BFS
DFS
Topological sort
in a DG is a node labelling such that:
Strongly connected components
Heap
Sorted arrays
Balanced binary search trees
Search tree property
Hash tables
Bloom filters
Python list - time complexity
(https://wiki.python.org/moin/TimeComplexity) avg case, amortized worst case (CPython impl.): Copy O(n), O(n) Append O(1), O(1) Pop last O(1), O(1) Pop intermediate O(k), O(k) Insert O(n), O(n) Get Item O(1), O(1) Set Item O(1), O(1) Delete Item O(n), O(n) Iteration O(n), O(n) Get Slice O(k), O(k) Del Slice O(n), O(n) Set Slice O(k+n), O(k+n) Extend O(k), O(k) Sort O(n log n), O(n log n) Multiply O(nk), O(nk) x in s O(n), min(s), max(s) O(n), Get Length O(1), O(1)
Python dict/set - time complexity
(https://wiki.python.org/moin/TimeComplexity) avg case, amortized worst case (CPython impl.): Copy O(n), O(n) Get Item O(1), O(n) Set Item O(1), O(n) Delete Item O(1), O(n) Iteration O(n), O(n) (set) x in s O(1), O(n)
Python deque - time complexity
(https://wiki.python.org/moin/TimeComplexity) avg case, amortized worst case (CPython impl.): Copy O(n), O(n) append O(1), O(1) appendleft O(1), O(1) pop O(1), O(1) popleft O(1), O(1) extend O(k), O(k) extendleft O(k), O(k) rotate O(k), O(k) remove O(n), O(n)
Python open() - buffering
the default buffering policy for open():
- binary files will use the default buffer size of the host system
- text files will use a TextIOWrapper only if marked with attribute isatty (interactive tty or terminal)
otherwise specify explicitly the buffer size (> 1, in bytes) for binary files or 1 for text files