Every problem can have multiple solutions
When thinking about the various solutions on the table, we need to be able to pick the best one.
Simply choosing 1 or 2 data sets and running them against the implemented algorithms doesn’t really prove anything
When thinking about which algorithm is better, we can look at that from various perspectives. Different metrics we could use include:
We will focus on Time Efficiency.
We use the Random Access Machine (RAM) Model of computation to analyze algorithms separate from implementing them in a particular programming language.
Back to Problem P with 2 or more viable solutions A and B:
We can develop a T(n) for A and a T(n) for B.
For example:
$$ T_A(n)=2n^2+3n+4
$$
$$ T_B(n)=4n+10 $$
Big O
Big O analysis allows us to disregard coefficients and lower order terms of our T(n) functions.
We end up with a list of categories that most algorithms fall into:
O(1)
- ConstantO(lg n)
- LogarithmicO(n)
- LinearO(n lg n)
- Log LinearO(n^2)
- QuadraticO(n^3)
- CubicO(2^n)
- ExponentialSome Examples
//This example is O(n)
int s = 0;
for (int i = 0; i < n; i++)
s++;
//This example is O(n^2)
int s = 0;
for (int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
s++;
//This example is O(lg n)
int s = 0;
for (int i = 1; i < n; i *= 2)
s++;
//This example is O(n lg n)
int s = 0;
for (int i = 0; i < n; i++)
for(int j = n; j >= 1; j /= 2)
s++;