15.3 For Loops
Count, count, count...
Now that we've seen some runtime analysis, let's work through some more difficult examples. Our goal is to get some practice with the patterns and methods involved in runtime analysis. This can be a tricky idea to get a handle on, so the more practice the better.
Scan Example:
Last time, we saw the function dup1, that checks for the first time any entry is duplicated in a list:
int N = A.length;
for (int i = 0; i < N; i += 1)
for (int j = i + 1; j < N; j += 1)
if (A[i] == A[j])
return true;
return false;
We have two ways of approaching our runtime analysis:
Counting number of operations
Geometric visualization
Method 1: Count Number of Operations
Since the main repeating operation is the comparator, we will count the number of "==" operations that must occur.
The first time through the outer loop, the inner loop will run times. The second time, it will run times. Then , , .... all the way till running the inner loop exactly time when i = . In the worst case, we have to go through every entry, and the outer loop runs times.
Then, let = total number of "==" operations that have occurred. The number of comparisons is:
where is part of the family.
Since "==" is a constant time operation, the overall runtime in the worst case is .
Method 2: Geometric Visualization
We can also approach this from a geometric view.
Let's draw out when we use == operations in the grid of combinations:

We see that the number of == operations is the same as the area of a right triangle with a side length of . Since area is in the family, we see again that the overall runtime is .
Last updated