Determine the frequency counts for all statement in the following algorithm Segment
Determine the frequency counts for all statement in the following algorithm Segment.
I=1;
While(I<=n)
{
X=X+I;
I=I+1;
}Determine the frequency counts for all statement in the following algorithm Segment.
I=1;
While(I<=n)
{
X=X+I;
I=I+1;
}Total frequency count = \(3n + 2\), so the time complexity is \(O(n)\).
| Statement | Frequency count |
|---|---|
I = 1; | 1 |
while (I <= n) | \(n + 1\) |
X = X + I; | \(n\) |
I = I + 1; | \(n\) |
| Total | \(3n + 2\) |
Why:
\[1 + (n + 1) + n + n = 3n + 2 = O(n)\]
Log in or create a free account to answer this question.