|
Expansion
|
Summation
|
Big Oh
|
|
1+2+3+4+....+ N
|
N(N+1)/2
|
O(N^2)
|
|
N+N+N+....+N
|
N*N
|
O(N^2)
|
|
N+N+N+....+N+....+N+....+N
|
3N*N
|
O(N^2)
|
|
1+2+4+....+
|
2^(N+1)-1
|
O(2^N)
|
|
Arrays
|
ArrayLists
|
|
They have a fixed size
|
Size can change
|
|
Much faster to add to
|
Adding to an arraylist is usually N
|
|
But when you reach the max, the computer doubles the limit every time you hit the limit so it takes O(N) times --> This is why it takes longer
|
public int howLong(String [] connects, String [] costs){
int [] [] adjMatrix = new int [connects.length][connects.length]'
for (int i =0; i<connects.length; i++){
String [] edges - connects[i[.split(" ");
String [] weights = costs[i].split(" ");
for (int j =0; j<edges.length; j++){
adjMatrix[i][Integer.partseInt(edges[j])) = Integer.parseInt(weights[j]);
|
Empirical Analysis
|
Mathematical Analysis
|
|
Measure running times, plot, and fit curve
|
Analyze algorithm to estimate # ops as a function of input size
|
|
Easy to perform experiments
|
May require advanced mathematics
|
|
Model useful for predicting, but not for explaining
|
Model useful for predicting and explaining
|
|
Mathematical analysis is independent of a particular machine or compiler; applies to machines not yet built.
|
|
Linked List
|
ArrayList
|
Both
|
|
|
Separate elements in memory that all have pointers to each other
|
A collection of elements in order in memory
|
Collection of elements
|
|
|
Add, remove, for loops, sort themselves, clear
|
|||
|
Remove First Element:
|
N Time because you don't have to shift something when it is in the front of the list
|
N^2 Time because everything stores sequentially so when you take something out you have to shift everything by 1
|
|
|
Remove Middle Index
|
Has a higher coefficient and thus is slower: To get there takes time but to remove it is instantaneous :O(N)
|
Faster: To get to middle element is instantanenous but to remove it you have to shift it: O(N)
|
|
|
Best for adding/removing front
|
Best for adding/removing something from back/middle
|
||
|
Class
|
TopMatch
|
TopMatches
|
|
BruteAutocomplete
|
O(n)
|
O(mlogm + n)
|
|
BinarySearchAutocomplete
|
O(log(n) + m)
|
O(log(n) + (m + k)log(k))
|
|
TrieAutocomplete
|
O(w)
|
O(w)
|
public ListNode<Integer> convertRec (ListNode<String> list):
if (list == null) return null;
return new ListNode<Integer> (list.info.length, convertRev(list.next);
List Node first = new ListNode <"cherry", null, null>;
List Node fig = new ListNode <"fig", first, null>;
List Node mango = new ListNode <"mango", fig, null>;
first.right = fig;
fig.right = mango;
public int howLong(String [] connects, String [] costs){
int [] [] adjMatrix = new int [connects.length][connects.length]'
for (int i =0; i<connects.length; i++){
String [] edges - connects[i[.split(" ");
String [] weights = costs[i].split(" ");
for (int j =0; j<edges.length; j++){
adjMatrix[i][Integer.partseInt(edges[j])) = Integer.parseInt(weights[j]);
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.