Big O Flashcards
What is Big O?
The language and metric we use to describe the efficiency of algorithms
What is best case runtime?
If all elements are equal, then quick sort will, on average, just traverse the array once. This is O(N).
What is worst case runtime?
Getting really unlucky and having to traverse the entire array many times…This is O(N^2).
What is expected case runtime?
If the worst case doesn’t happen over and over again…this is O(N log N).
What are the steps in determining runtime?
- Drop the Constants.
- Drop the non-dominant terms.
- Consider multi-part algorithms (add vs multiply).
Which one is faster?
Min and Max1 1. int min = Integer.MIN_VALUE; 2. int max = Integer.MIN_VALUE; 3. for (int x : array) { if ( x < min ) min = x; if ( x > max) max = x; }
Min and Max2 1. int min = Integer.MIN_VALUE; 2. int max = Integer.MIN_VALUE; 3. for (int x : array) { if ( x < min ) min = x; } 4. for ( int x : array) { if ( x > max) max = x; }
What is the hierarchy of dominant terms?
- O(n!)
- O(2^n)
- O(n^2)
- O(n log n)
- O(n)
- O(log n)
What is an example of O(log N) runtime?
Binary search algorithm…when you see a problem where the number of elements in the problem space gets halved each time.
Formally speaking, what is the formula for Big-O notation?
If F and G are functions, then F = O(G) if there exists some constant, C, such that |F(x)| <= C * |G(x)| for sufficiently large x.
In the equality case, when |F(x)| = C * |G(x)| for sufficiently large x, f = O(g).