Reading Big-Oh from Code
We have the notation now, and we have the two rules that go with it: suppress the coefficients, drop the lower-order terms. So far we have applied those rules to a step count we worked out by hand, turning an expression like into .
In practice we almost never count steps. Once you know what the two rules do to a step count, you can look at the code, reason about how its work grows, and name the Big-Oh without writing down a step count.
The procedure is the same every time. Find where most of the work happens, which is usually a loop or a nest of loops. Then ask how many times that work runs as the input grows. Constant work repeated times is . Constant work repeated times is . The number of operations in the loop body affects only the coefficient, and we suppress the coefficient anyway, so we do not have to count them. The next few sections are worked examples of this procedure.
Before you write the Big-Oh for an algorithm, you must figure out what the input size is because the Big-Oh is a function of that size.
In every example so far except one, the size of the input has been the length of the array we passed in, and we denote it by . That is the usual case when we study data structures: the size of the input is the number of elements the structure holds. But it is not always the case.
The one exception so far is the countUp method. It takes a single integer num and loops num times. We still used to denote the value of num, but that is the value of a single integer, and the work grows with that value.
Measuring the size of a number
In more formal settings, the “size” of a single integer is often the number of bits needed to represent it, which is about , where is the numeric value. Here we are using the common convention of measuring the work as a function of the numeric value itself, assuming ordinary integer operations take constant time.
Multiple input sizes
We may need more than one variable to capture the size of the input. For example, when we get to graph algorithms, we often use two variables for the size of the input: the number of vertices and the number of edges.