Respuesta :
Answer:
Check the explanation
Explanation:
Let algorithm be Called minSecondMin(A, p, q) where A is input array with starting index p and ending index q. q-p+1 = n
pseudoCode in C style:-
minSecondMin (A,p,q)// suppose it takes T(n) time
{
if(p +1 == q)
{
if(a[p]>a[q])// here one comparison is done
{
min = a[q]
secondMin = a[p]
}
else
{
min = a[p]
secondMin = a[q]
}
}
else
{
m = floor (p+q/2)
(min1, secondMin1) = minSecondMin (A, p, m)// it takes T(n/2)
(min2, secondMin2) = minSecondMin (A, m+1,q)//it takes T(n/2)
if(min1 > min2)// here 1 comparison
{
min = min2
if(min1>secondMin2)//here 1 comparison
secondMin = secondMin2
else
secondMin = min1
}
else
{
min = min1
if(min2>secondMin1)//here 1 comparison
secondMin = secondMin1
else
secondMin = min2
}
}
}
Recurrence relation for time;-
T(n) = O(1) , if n = 2
= 2T(n/2) + c , if n> 2
Recurrence relation for number of comparisons:-
Let number of comparison = C(n)
Then C(n) = 1 , if n = 2
= 2C(n/2) + 2 , if n > 2
C(n) = 2C(n/2) + 2
Solving using substitution method we get
= 2(2C(n/4) + 2) + 2
= [tex]2^2[/tex]C(n/4) + [tex]2^2[/tex] + 2
= [tex]2^3[/tex] C(n/8) + [tex]2^3[/tex] + [tex]2^2[/tex] + 2
Continuing this k times
= [tex]2^k[/tex]C(n/[tex]2^k[/tex]) + [tex]2^k[/tex] + [tex]2^{k-1[/tex] + [tex]2^{k-2[/tex] + ...+ [tex]2^2[/tex] + 2
It will stop when n/[tex]2^k[/tex]= 2 so k = log2n - 1
= [tex]2^{log_{2}n - 1} C(1) + 2(2^{k-1+1} - 1)[/tex]
= [tex]2^{log_{2}(n/2)} + 2^{k+1} - 2[/tex]
= [tex]n/2 + 2^{log_{2}n} -2[/tex]
= n/2 + n - 2
= 3n/2 - 2
So, required number of comparisons = 3n/2 - 2