Next: Sorting techniques. Up: Computational complexity. Previous: None.
Big-O notation is a technique for quickly approximating the speed of a procedure. Basically, it identifies whether the running time grows exponentially with the input size, or whether it grows linearly, or quadratically, or whatever.
We can define the time to run a procedure as T(n), a function T of the input size n. Big-O notation would write something like the following.
Formally, the notation T(n) = O(g(n)) means the following: There is some constant c and some input size N, so that for any input sizes n beyond N,
For example, consider the following code that determines whether a number n is prime.
int i;
for(i = 2; i * i <= n; i++) {
if(n % i == 0) {
IO.println("not prime");
break;
}
}
if(i * i > n) IO.println("is prime");
This procedure turns out to take
O(n0.5) time:
The total number of iterations can be bounded by sqrt(n), and
the amount of time for each iteration is bounded by a constant.
There are three helpful rules that you might keep in mind when you're trying to evaluate the speed of a program.
In our primality example, we would reason like the following.
O(1) time
for(...) {
O(1) time
}
O(1) time
Each of the blocks replaced by ``O(1) time'' involved no
loops or function calls.
O(1) time O(sqrt(n)) time O(1) time
// exponentation, method 1: finds a to the bth power
ret = 1.0;
for(int i = b; i > 0; i--) ret *= a;
IO.println(ret);
// exponentiation, method 2: finds a to the bth power
ret = 1.0;
int i = n;
while(i > n) {
if(i % 2 == 0) {
i = i / 2;
} else {
ret *= a;
i = (i - 1) / 2;
}
a = a * a;
}
IO.println(ret);
// count lattice points: determines how many integer-coordinate
// points lie in a circle of radius r
count = 0;
y = 0;
x = r;
while(x > 0) {
while(x*x + y*y < r*r) {
++y;
}
count += y;
}
IO.println(4 * count + 1);
This last example illustrates an example where the ``basic rules'' outlined above give a poor solution: You're better off reasoning through the problem more carefully.
Next: Sorting techniques. Up: Computational complexity. Previous: None.