Question: Consider the algorithm Alg 1 described below in pseudo - code. Alg 1 takes as input an array ( whose elements are either 0 or

Consider the algorithm Alg1 described below in pseudo-code. Alg1 takes as input an array
(whose elements are either 0 or 1) and an integer (the size of the array). The index of the first
element of the array is 1.
Alg1(A,n)
2.
num 1larr0
num 0larr0
ilarr1
while in do
if A[i]=1 then
num1 larr num 1+1
if num1n2 then
return TRUE
endif
else
num0 larr num0+1
if num0>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.
 Consider the algorithm Alg1 described below in pseudo-code. Alg1 takes as

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!