edited by
1,255 views
5 votes
5 votes

Simple linear search to find max min algo

maxmin(a,n,max,min)
{
   max=min=a[1];
   for i=2 to n do
   {
     if a[i]>max then
         max:=a[i]; 
     else if a[i]<min then 
         min:=a[i];
    }
}

1.Average case complexity of the above algo given that the first if conditions fails for n/2 elements

2.Average case complexity of the above algo if the first condition fails 1/2 times

plz xplain

edited by

1 Answer

Best answer
5 votes
5 votes
Complexity of the algorithm is $O(n)$ and is irrespective of the success of if case as both if as well as else is $O(1)$ operation.
If you say exactly, the complexity in terms of comparisons will be

1. $n-n/2-1$ (number of elements for which first if succeeds) $+ 2 \times (n/2) $ (number of elements for which first if fails)

$= 3n/2 -1$

2. $ (n-1)/2 + 2\times (n-1)/2$
$=3(n-1)/2$
edited by

Related questions

3 votes
3 votes
1 answer
1
sushmita asked Mar 23, 2017
2,690 views
CAN SOMEONE SOLVE THE NUMBER OF COMPARISIONS FOR COMPUTING MIN AND MAX IN AN ARRAY USING DIVIDE N CONQUER??RECURRENCE RELATION IS$ T(n) = 2 T(\frac{n}{2}) + 2 $IT SHOULD ...
0 votes
0 votes
1 answer
3
iita asked Jan 24, 2017
1,135 views
The minimum number of comparisons required to find the minimum and maximum of 60 numbers is____
1 votes
1 votes
0 answers
4
Anjana Babu asked Jan 10, 2017
1,279 views
Please explain using images how to convert BST into max/min heap in-place .Please explain the complexity of doing so.