Determine the precise Big-Oh values for the following code sample, based on the number of statement executions.
Keep the following considerations in mind:
- Remember to consider each statement in compound statements separately.
- Pay close attention to the initial and end values of loop variables!
- Loops such as “for” and “while” contain an implicit “jump” instruction that causes execution to proceed from the end of the loop back to the beginning of the loop.
Code sample:
for (int i = 0; i < n; i++){
for (int j = i; j < n; j++){
sum += i;
}
}
The answer above is O(4N^2 + 5N + 2), but I am not entirely sure how the answer is counted. I am trying to better understand how to count the executions.