Data Structures & Algorithms Cheatsheet
Data Structures & Algorithms Cheatsheet
We also summarize some of the mathematics useful in the analysis of algorithms, including commonly encountered functions;
useful formulas and
approximations; properties of logarithms;
asymptotic notations; and solutions to divide-and-conquer recurrences.
Sorting.
The table below summarizes the number of compares for a variety of sorting
algorithms, as implemented in this textbook.
It includes leading constants but
ignores lower-order terms.
partially-sorted arrays
rarely useful;
bubble sort Bubble.java ✔ ✔ n ½n2 ½n2
n log n guarantee;
mergesort Merge.java ✔ ½ n lg n n lg n n lg n
stable
fastest in practice
n log n guarantee;
heapsort Heap.java ✔ n† 2 n lg n 2 n lg n
in place
Priority queues.
The table below summarizes the order of growth of the running time of
operations for a variety of priority queues, as implemented in this textbook.
It ignores
leading constants and lower-order terms.
Except as noted, all running times are worst-case running times.
Symbol tables.
The table below summarizes the order of growth of the running time of
operations for a variety of symbol tables, as implemented in this textbook.
It ignores
leading constants and lower-order terms.
red-black BST
RedBlackBST.java log n log n log n log n log n log n
(left-leaning)
(separate-chaining)
hash table
(linear-probing)
LinearProbingHashST.java n n n 1† 1† 1†
Graph processing.
The table below summarizes the order of growth of the worst-case running time and memory usage (beyond the memory for the graph itself)
for a variety of
graph-processing problems, as implemented in this textbook.
It ignores leading constants and lower-order terms.
All running times are worst-case running times.
∗ ∗
iterated binary logarithm lg x 0 if x ≤ 1; 1 + lg (lg x) otherwise
factorial n! 1 × 2 × 3 × … × n
binomial coefficient ( )
n n!
k
k! (n−k)!
Harmonic sum:
1 + 1/2 + 1/3 + … + 1/n ∼ ln n
Triangular sum:
1 + 2 + 3 + … + n = n (n + 1) / 2 ∼ n
2
/2
Sum of squares:
12 + 2
2
+ 3
2
+ … + n
2
∼ n
3
/3
Geometric sum:
If r ≠ 1 , then
1 + r + r2 + r
3
+ … + r
n
= (r
n+1
− 1) / (r − 1)
r = 1/2 :
1 + 1/2 + 1/4 + 1/8 + … + 1/2n ∼ 2
r = 2 :
1 + 2 + 4 + 8 + … + n/2 + n = 2n − 1 ∼ 2n , when n is a power of 2
Stirling's approximation:
lg(n!) = lg 1 + lg 2 + lg 3 + … + lg n ∼ n lg n
Exponential:
(1 + 1/n)n ∼ e; (1 − 1/n)
n
∼ 1/e
n
Binomial coefficients:
( k) ∼ n
k
/ k! when k is a small constant
n n n+1
Properties of logarithms.
Definition: logb a = c means bc = a .
We refer to b as the base of the logarithm.
Finite product:
logb (x1 × x2 × … × xn ) = logb x1 + logb x2 + … + logb xn
sequential search
for (int i = 0; i < n; i++)
Linear O(n) grade-school addition op();
op();
Factorials:
n! is 2Θ(n log n) .
f (n)
Limits:
If lim = c
for some constant 0 < c < ∞ , then
f (n) is Θ(g(n)) .
n→∞ g(n)
f (n)
Limits:
If lim = 0 ,
then f (n) is O(g(n)) but not Θ(g(n)) .
n→∞ g(n)
f (n)
Limits:
If lim = ∞ ,
then f (n) is Ω(g(n)) but not O(g(n)) .
n→∞ g(n)
FUNCTION o(n )
2
O(n )
2
Θ(n )
2
Ω(n )
2
ω(n )
2
∼ 2n
2
∼ 4n
2
log
2
n ✔ ✔
10n + 45 ✔ ✔
2n
2
+ 45n + 12 ✔ ✔ ✔ ✔
4n
2 −
−
− 2√n ✔ ✔ ✔ ✔
3n
3
✔ ✔
2
n
✔ ✔
Divide-and-conquer recurrences.
For each of the following recurrences we assume T (1) = 0
and that n / 2 means either ⌊n / 2⌋ or
⌈n / 2⌉ .
T (n) = 2T (n / 2) + n ∼ n lg n mergesort
T (n) = T (n − 1) + n ∼
1
n
2
insertion sort
2
T (n) = 2T (n − 1) + 1 ∼ 2
n
towers of Hanoi
T (n) = 7T (n / 2) + Θ(n )
2
Θ(n
log2 7
) = Θ(n
2.81...
) Strassen multiplication
Master theorem.
Let a ,
≥ 1 b ≥ 2 , and c > 0 and suppose that
T (n) is a function on the non-negative integers that satisfies
the divide-and-conquer recurrence
c
T (n) = a T (n / b) + Θ(n )
If c < log
b
, then T (n)
a = Θ(n
log b
a
)
c
If c = logb a , then T (n) = Θ(n log n)