Primary pratical reason: avoid performance bugs
Predict performance
Compare algorithms
Provide guarantees
Understand theoretical basis
Use sicentific method to understand performance
Observe some feature of the natural world, generally with precise measurements.
Hypothesize a model that is consistent with the observations.
Predict events using the hypothesis.
Verify the predictions by making further observations.
Validate by repeating until the hypothesis and observations agree.
Principles
experiments: reproducible
hypotheses: falsifiable
Observations
order of growth
description | fuction |
---|---|
constant | 1 |
logarithmic | logN |
linear | N |
linearithmic | NlogN |
quadratic | N^2 |
cubic | N^3 |
exponetial | 2^N |
3-Sum problem:
Given N distinct integers, how many triples sum to exactly zero?
brute-force algorithm: N^3
T(N ) = aN^b
int N = a.length;
int count = 0;
for (int i = 0; i < N; i++)
for (int j = i + 1; j < N; j++)
for (int k = j + 1; k < N; k++)
if (a[i] + a[j] + a[k] == 0)
count ++;
return count;
Binary search: lgN
int lo = 0, hi = a.length - 1
while(lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
}
An N^2logN algorithm for 3-Sum:
Sort the N(distinct) numbsers: N^2
Binary search for -(a[i] + a[j]): N^2logN
Analyses
Best case. Lower bound on cost
Worst case. Upper bound on cost
Average case. Expected cost for random input
Commonly-used notations in the theory of algorithms
Big Theta
Big Oh
Big Omega
Memory
type | bytes |
---|---|
boolean | 1 |
byte | 1 |
char | 2 |
int | 4 |
float | 4 |
long | 8 |
double | 8 |
char[] | 2N+24 |
int[] | 4N+24 |
double[] | 8N+24 |
char[][] | 2MN |
int[][] | 4MN |
double[][] | 8MN |