Question: Alg 1 ( A , n ) 2 . 3 . num 1 larr 0 4 . num 0 larr 0 5 . ilarr 1

Alg1(A,n)
2.
3. num 1larr0
4. num 0larr0
5. ilarr1
6.
7. while in do
if A[i]=1 then
num1 larr num 1+1
if num1n2 then
return TRUE
endif
else
num 0larr num 0+1
if num 0>n2 then
return FALSE
endif
endif
ilarri+1
endfor
When estimating the run-time complexity of Alg1 as a function of the size of the input array consider only the number of array comparisons performed when the algorithm is run on the input array. (Note that array comparisons only occur on line 8 of Alg1.)
(5 marks) State the worst-case running time complexity of Alg1 as a function of the size of the input array. Do not use the asymptotic notation. Explain your answer (show your calculations).
(6 marks) State the average case complexity of Alg1 as a function of the size of the input array. Explain your answer using the methodology presented in class, i.e., define an appropriate sample space, state a probability distribution function (assume a uniform distribution), define the necessary random variables, etc. Show your calculations.8
 Alg1(A,n) 2. 3. num 1larr0 4. num 0larr0 5. ilarr1 6.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!