- #1
s3a
- 818
- 8
Hello to everyone who's reading this.
The problem that I'm struggling with, and it's solution, are typed below.:
PROBLEM STATEMENT:
Assume an array [0, ... n - 1] of int, and assume the following code segment:
What is worstTime(n)?
* Addition and multiplication
* O(n) means |f(n)| <= C*g(n) for all n >= n_0, where n_0 is a placeholder for an integer, and n is a variable that can hold a positive integer. (Generally, I believe the input can be a non-integer real number, but not in the context of the problem that this thread is about.)
OFFICIAL SOLUTION:
-----------------------------------------------------------------------------------------
| Statement | Worst case Number of Executions |
-----------------------------------------------------------------------------------------
| i = 0 | 1 |
-----------------------------------------------------------------------------------------
| i < n - 1 | n |
-----------------------------------------------------------------------------------------
| i++ | n - 1 |
-----------------------------------------------------------------------------------------
| a > a[i+1] | n - 1 |
-----------------------------------------------------------------------------------------
| System.out.println() | n - 1 |
-----------------------------------------------------------------------------------------
MY CONFUSION(S):
What I am confused about is (at least) the following two things.:
1:
Why is each time i < n - 1 is run, considered to be one instruction (to be executed), instead of two? Isn't it the case that the subtraction operator and the less-than operator are each considered to be one instruction (to be executed), such that the the total number of instructions (to be executed) of i < n - 1 should be 2*n, instead of n? I have a similar confusion with a > a[i+1] being counted as one instruction (to be executed), rather than 2, such that the total instruction (to be executed) of a > a[i+1] seems to me that it should be 2*(n-1), rather than n - 1.
I get that, even if the solution was changed to what I'm saying above, the order of the worst-case running time would remain the same, as O(n), but could someone still please tell me if it's a mistake in the solution, or if it's not a mistake, could I please have the flaw in my logic pointed out?
2:
Do all instructions (to be) executed) have the same weight? In other words, is each instruction (to be executed) equally demanding for the processor to process (assuming that there is no shortage of any resources)? If not, then why do we treat them all equivalently, as having a weight of 1?
Any input would be GREATLY appreciated!
The problem that I'm struggling with, and it's solution, are typed below.:
Homework Statement
PROBLEM STATEMENT:
Assume an array [0, ... n - 1] of int, and assume the following code segment:
Code:
for (int i = 0; i < n - 1; i++)
if(a[i] > a[i+1])
System.out.println(i);
What is worstTime(n)?
Homework Equations
* Addition and multiplication
* O(n) means |f(n)| <= C*g(n) for all n >= n_0, where n_0 is a placeholder for an integer, and n is a variable that can hold a positive integer. (Generally, I believe the input can be a non-integer real number, but not in the context of the problem that this thread is about.)
The Attempt at a Solution
OFFICIAL SOLUTION:
-----------------------------------------------------------------------------------------
| Statement | Worst case Number of Executions |
-----------------------------------------------------------------------------------------
| i = 0 | 1 |
-----------------------------------------------------------------------------------------
| i < n - 1 | n |
-----------------------------------------------------------------------------------------
| i++ | n - 1 |
-----------------------------------------------------------------------------------------
| a > a[i+1] | n - 1 |
-----------------------------------------------------------------------------------------
| System.out.println() | n - 1 |
-----------------------------------------------------------------------------------------
MY CONFUSION(S):
What I am confused about is (at least) the following two things.:
1:
Why is each time i < n - 1 is run, considered to be one instruction (to be executed), instead of two? Isn't it the case that the subtraction operator and the less-than operator are each considered to be one instruction (to be executed), such that the the total number of instructions (to be executed) of i < n - 1 should be 2*n, instead of n? I have a similar confusion with a > a[i+1] being counted as one instruction (to be executed), rather than 2, such that the total instruction (to be executed) of a > a[i+1] seems to me that it should be 2*(n-1), rather than n - 1.
I get that, even if the solution was changed to what I'm saying above, the order of the worst-case running time would remain the same, as O(n), but could someone still please tell me if it's a mistake in the solution, or if it's not a mistake, could I please have the flaw in my logic pointed out?
2:
Do all instructions (to be) executed) have the same weight? In other words, is each instruction (to be executed) equally demanding for the processor to process (assuming that there is no shortage of any resources)? If not, then why do we treat them all equivalently, as having a weight of 1?
Any input would be GREATLY appreciated!