Big-Oh from Loop Structure

For each method below, give the Big-Oh of its worst-case running time. Unless the question says otherwise, every array has length .

Solution

. The two loops run one after the other, so their work adds: , and the is a suppressed coefficient.

Solution

. The loops are nested, so their work multiplies. The inner comparison runs once for every pair (i, j), which is times.

Solution

The comparison runs times. That is . Suppress the coefficients, drop the lower-order term, and the method is .

Solution

There is no array. The work grows with the value of num, so is that value. Each pass divides num by , and dividing by a constant on every pass means the loop runs a logarithmic number of times. The method is .

Solution

. The body of allPresent has one loop, but that loop’s body is not constant work: it calls contains, which is . An call made times is .