I'm trying to figure out how to fill out this chart, i.e. how many times each line runs. When I do tracing, I know that the inner loop runs i+1 times, but I also don't know how to correlate that to the "Times" section in the following chart.
Cost Times
void foo( Array A, int n) {
1. for(int i = 1 ; i <= n ; i*=2) c1 ?
2. for(int j = n ; j > n-i ; j--) c2 ?
3. cout << A[j] << endl; c3 ?
My tracing when n = 10:
+-----+----+-----------+-----------------+
| i | j | j > n-i | Total run times |
+-----+----+-----------+-----------------+
| 1 | 10 | 10 > 10-1 | |
| | 9 | 9 > 9 | 2 |
| 2 | 10 | 10 > 10-2 | |
| | 9 | 9 > 8 | |
| | 8 | 8 > 8 | 3 |
| 4 | 10 | 10 > 10-4 | |
| | 9 | 9 > 6 | |
| | 8 | 8 > 6 | |
| | 7 | 7 > 6 | |
| | 6 | 6 > 6 | 5 |
| ... | | | i+1 |
+-----+----+-----------+-----------------+