Consider functions and expressed in pseudocode as follows:
Function_1
while n>1 do
for i=1 to n do
x = x + 1;
end for
n = ⌊n/2⌋;
end while
| Function_2
for i = 1 to 100 * n do
x = x + 1;
end for
|
Let and denote the number of times the statement is executed in and respectively.
Which of the following statements is/are
View worked solution
Answer: A;D
To determine the correct options, let us analyze the execution counts and for the statement x = x + 1 in both functions.
Analysis of :
In each iteration of the outer while loop, the value of is halved. The inner for loop runs times. Therefore, the total number of executions is given by the sum of a geometric progression:
Using the properties of geometric series, we have:
Thus, .
Analysis of :
The loop runs exactly times. Thus, the statement is executed: This clearly means .
Evaluating the Options:
- Option A: Since both and are in , they are asymptotically equivalent. Hence, is TRUE.
- Option B: is false because does not grow strictly slower than (their ratio is a non-zero constant ).
- Option C: is false because does not grow strictly faster than .
- Option D: Since , it also satisfies the upper bound , which is TRUE.
Free practice · Your answers on this page are not saved.
