given a sorted array b, store in i index of v
i = -1; k = b.length;
while (i != k-1) {
int m = (i + k)/2;
if (b[m] <= v) { i = m;
} else { k = m;}}
Back
insertion sort stability
Front
Yes
Back
interfaces consist of:
Front
public abstract methods
Back
LinkedList contains(v) complexity
Front
O(n)
Back
basic step
Front
a step whose time it takes to evaluate does not depend on the size of the data on which it operates; takes constant time
Back
selection sort average complexity
Front
O(n^2)
Back
quick sort stability
Front
No
Back
interface comparable
Front
returns a negative int, 0, or positive int as this object is less than, equal to, or greater than ob;
class must implement: int compareTo(T ob);
Back
4 Loopy Questions
Front
1. Does it start right? Does the initialization make the invariant true?
2. Does it end right? Is the postcondition .true upon termination?
3. Does the repetend make progress toward termination?
4. Does the repetend keep the invariant true?
Back
merge sort space complexity
Front
O(n)
Back
linear search
Front
given an array b, it is known that value v is in it; store in k the index of the first occurrence of v in b
k = 0;
while (b[k] != v) { k = k+1;}
return k;
given an array b, unknown whether value v is in it; store in k the index of the first occurrence of v in b
i = -1; k= b.length();
while (i+1<k) {
if (b[i+1] == v) { return i+1; }
Back
interface iterable
Front
implementing this interface allows an object to be the target of a for-each loop; requires declaring method iterator
method = iterator()
ex. public class Stack<E> implements Iterable<E> {
public abstract Iterator<T> iterator();
Back
merge sort worst-case complexity
Front
O (n log n)
Back
quick sort worst-case complexity
Front
O(n^2)
Back
linear search average complexity
Front
O(n)
Back
merge sort
Front
if (h >= k) { return; }
int e = (h+k)/2;
mergeSort(b, h, e);
mergeSort(b, e+1, k);
merge(b, h, e, k);
Back
merge sort stability
Front
Yes
Back
interface iterator
Front
an object that provides methods to write a loop to enumerate & process each element
methods = hasNext() and next()
- hasNext(): returns boolean for if there is a next item
- next(): always starts with call of hasNext(); return next variable
- constructor must initialize field n to the index
Back
quick sort
Front
int h1=h; int k1=k;
while (b[h1..k1] has more than 1 element) {
int j = partition(b, h1, k1);
if (b[h1..j-1] is smaller than b[j+1..k1]) {
QS(b, h, j-1); h1 = j+1; }
else { QS(b, j+1, k1); k1 = j-1; }
Back
LinkedList poll() complexity
Front
O(n)
Back
Array add() complexity
Front
O(n)
Back
List<T> methods
Front
get(int), indexOf(int), add(int, T)
Back
Map<K,V> methods
Front
put(k,v), get(object)
Back
insertion sort space complexity
Front
O(1)
Back
binary search average complexity
Front
O(log n)
Back
insertion sort
Front
k = 0;
while (k != b.length) {
j = k;
while (k > 0 && b[k] < b[k-1]) {
swap b[j-1] and b[j];
j = j-1; }
k = k + 1; }
Back
quick sort average complexity
Front
O(n log n)
Back
heap sort average complexity
Front
O(n log n)
Back
Array get(int i) complexity
Front
O(1)
Back
quick sort space complexity
Front
O(log n)
Back
insertion sort worst-case complexity
Front
O(n^2)
Back
when time complexity is log n
Front
1. when the variable that is within the loop guard double with every iteration
2. recursive problems where n gets split in half each iteration
Back
heap sort worst-case complexity
Front
O(n log n)
Back
heap sort
Front
Make array into a max heap, then poll, and repeat
for (int k = 1; k < b.length; k = k+1) {
bubbleUp(b,k); }
for (int k = b.length-1; k > 0; k = k-1) {
swap b[0] and b[k];
bubbleDown(b,k); }
Back
quick sort partition average complexity
Front
O(n)
Back
quick sort partition
Front
j = h; t = k;
while (j < t) {
if (b[j+1} <= b[j]) { swap b[j+1] and b[j]; j = j+1;
} else { swap b[j+1] and b[t]; t = t-1; }}
Back
insertion sort average complexity
Front
O(n^2)
Back
selection sort stability
Front
No
Back
selection sort space complexity
Front
O(1)
Back
merge sort average complexity
Front
O(n log n)
Back
linear search worst-case complexity
Front
O(n)
Back
Collection<T> methods
Front
add(T), contains(object), isEmpty(), remove(object), size(), etc.
Back
binary search worst-case complexity
Front
O(log n)
Back
selection sort worst-case complexity
Front
O(n^2)
Back
selection sort
Front
k = 0;
while (k != b.length) {
j = k+1; i = k;
for (j = k+1; j < b.length; j = j+1) {
if (b[j] < b[i]) {
i = j; }
swap b[k] and b[i]; k = k+1; }
Back
definition of big-O
Front
function f(n) is in set O(g(n)) if there exists a positive constant c and N where for all n >= Nm f(n) <= c*g(n)
- O(g(n)) bounds the function f(n)
- f(n) grows like g(n) or slower
Back
LinkedList add(v) complexity
Front
O(1)
Back
Section 2
(50 cards)
HashSet add(v) complexity
Front
O(1)
Back
Adjacency Matrix best for:
Front
dense graphs
Back
recursive DFS space complexity
Front
O(V)
Back
iterative DFS uses:
Front
stack
Back
Binary Search Tree
Front
a binary tree with the property that for all parent nodes, the left subtree contains only values less than the parent, and the right subtree contains only values greater than the parent.
Back
sorted LinkedList contains(v) complexity
Front
O(n)
Back
Adjacency Matrix find an edge complexity
Front
O(1)
Back
tree depth
Front
length of path from root to node
Back
Adjacency Matrix space complexity
Front
O(|V|^2)
Back
preorder
Front
process root, then left, then right (stick on left)
Back
Heap add(v) complexity
Front
O(log n)
Back
iterative DFS & BFS list time complexity
Front
O(V+E)
Back
Heap poll() complexity
Front
O(log n)
Back
sorted LinkedList peek() complexity
Front
O(1)
Back
Adjacency List visit all edges complexity
Front
O(|V| + |E|)
Back
BFS
Front
Queue q = new Queue();
q.add(u);
while (q is not empty) {
u = q.remove();
if (u is not visited) {
visit u;
for each (u,v) {
if (v not encountered) {
mark v as encountered;
q.add(v); }}}
Back
iterative DFS & BFS matrix time complexity
Front
O(V^2)
Back
Heap peek() complexity
Front
O(1)
Back
Balanced BST poll() complexity
Front
O(log n)
Back
Adjacency List find an edge complexity
Front
O(|V| + |E|)
Back
Balanced BST peek() complexity
Front
O(log n)
Back
heap must:
Front
be a complete binary tree and have a heap-order (max-heap or min-heap)
Back
Adjacency List best for:
Front
sparse graphs
Back
HashSet get(int i) complexity
Front
O(1)
Back
BFS uses:
Front
queue
Back
Adjacency Matrix visit all edges complexity
Front
O(|V|^2)
Back
Binary Search Tree add(v) complexity
Front
O(height)
Back
Adjacency List space complexity
Front
O(|V| + |E|)
Back
Binary Tree contains(v) complexity
Front
O(n)
Back
max number of total nodes in binary tree
Front
2^(h+1) -1
Back
sorted LinkedList get(int i) complexity
Front
O(n)
Back
recursive DFS list time complexity
Front
O(V+E)
Back
complete binary tree
Front
every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
Back
sorted LinkedList poll() complexity
Front
O(1)
Back
sorted LinkedList add(v) complexity
Front
O(n)
Back
tree height
Front
length of longest path from the root to a leaf
Back
iterative DFS
Front
Stack s= new Stack();
s.push(u);
while (s is not empty) {
u = s.pop();
if (u not visited) {
visit u; }
for each edge (u,v) {
s.push(v); }}
Back
recursive DFS matrix time complexity
Front
O(V^2)
Back
DFS concept
Front
search entire paths at a time; like following a maze
Back
Balanced BST search complexity
Front
O(log n)
Back
BFS concept
Front
search each possibility at a level before moving on; like a search party fanning out
Back
BST search complexity
Front
O(n)
Back
min number of total nodes in binary tree
Front
height + 1
Back
Binary Search Tree contains(v) complexity
Front
O(height)
Back
inorder
Front
process left, then root, then right (stick down)
Back
postorder
Front
process left, then right, then root (stick on right)
Back
LinkedList peek() complexity
Front
O(n)
Back
max number of nodes at depth d in binary tree
Front
2^d
Back
Balanced BST add(v) complexity
Front
O(log n)
Back
HashSet contains(v) complexity
Front
O(1)
Back
Section 3
(24 cards)
iterative DFS & BFS space complexity
Front
O(V)
Back
Prim's (JPD's) method
Front
at each step, add edge with minimum weight, but keep edge connected to start node
Back
expected time for shortest path for dense graph
Front
O(n^2 log n)
Back
finding a hashed element with probing worst complexity
Front
O(n)
Back
load factor
Front
# of elements/ size of array
Back
finding a hashed element with chaining worst complexity
Front
O(n)
Back
chaining or addressing: worst memory?
Front
addressing
Back
finding a hashed element with chaining average complexity
Front
O(1)
Back
three steps to listen to events
Front
1. Have some class C implement an interface IN that is connected to the event
2. In class C, override method m to be called when event happens
3. Register an object of class C as a listener for the event
Back
Kruskal's method
Front
at each step, add edge with minimum weight; do not have to connect until end
Back
expected time for shortest path for sparse graph
Front
O(n log n)
Back
topological sort algorithm
Front
k = 0;
while there is a node of in-degree 0 {
let n be a node of in-degree 0;
give it number k;
delete n and all edges leaving it from the graph;
k = k+1; }
Back
dynamic resizing
Front
when load factor > 1/2
Back
anonymous functions notation
Front
(parameters) > (body)
Back
quadratic probing
Front
i, i+1^2, i+2^2, i+3^2, ...
Back
Dijkstra's theorem
Front
For a node f in F with minimum d value, d[f] is the length of the shortest v>s path
Back
time complexity of dynamic resizing
Front
constant amortized time
Back
spanning tree additive method
Front
must add n-1 edges
Back
spanning tree subtractive method
Front
must remove E-(n-1)
Back
anonymous functions relationship to interface with 1 method
Front
can use anonymous function instead of implementing interface with a class
Back
number of edges in spanning tree
Front
#V - 1
Back
Dijkstra's algorithm
Front
1. For a settled node s, a shortest path from v to s contains only settled nodes & d[s] = length of shortest v>s path
2. For a frontier node f, at least one v>f path contains only settled nodes and perhaps f, and d[f] = length of shortest such path
3. All edges leaving S go to F
Back
finding a hashed element with probing average complexity