03_Supplementary_notes_Time_complexity_analysis
03_Supplementary_notes_Time_complexity_analysis
In general, how can you determine the running time of a piece of code? The answer is that it depends on
what kinds of statements are used.
1. Sequence of statements
1. statement 1;
2. statement 2;
3. ...
4. statement k;
The total time is found by adding the times for all statements:
If each statement is "simple" (only involves basic operations) then the time for each statement is
constant and the total time is also constant: O(1).
In the following examples, assume the statements are simple unless noted otherwise.
2. if-then-else statements
1. if (condition) {
2. sequence of statements 1
3. }
4. else {
5. sequence of statements 2
6. }
Here, either sequence 1 will execute, or sequence 2 will execute. Therefore, the worst-case time is
the slowest of the two possibilities: max(time(sequence 1), time(sequence 2)). For example, if
sequence 1 is O(N) and sequence 2 is O(1) the worst-case time for the whole if-then-else statement
would be O(N).
3. for loops
1. for (i = 0; i < N; i++) {
2. sequence of statements
3. }
The loop executes N times, so the sequence of statements also executes N times. Since we assume
the statements are O(1), the total time for the for loop is N * O(1), which is O(N) overall.
4. Nested loops
First we'll consider loops where the number of iterations of the inner loop is independent of the
value of the outer loop's index. For example:
The outer loop executes N times. Every time the outer loop executes, the inner loop executes M
times. As a result, the statements in the inner loop execute a total of N * M times. Thus, the
complexity is O(N * M). In a common special case where the stopping condition of the inner
loop is j < N instead of j < M (i.e., the inner loop also executes N times), the total complexity
for the two loops is O(N2).
Now let's consider nested loops where the number of iterations of the inner loop depends on
the value of the outer loop's index. For example:
Now we can't just multiply the number of iterations of the outer loop times the number of
iterations of the inner loop, because the inner loop has a different number of iterations each
time. So let's think about how many iterations that inner loop has. That information is given in
the following table:
So we can see that the total number of times the sequence of statements executes is: