in Algorithms
29,398 views
64 votes
64 votes

An array of $n$ numbers is given, where $n$ is an even number. The maximum as well as the minimum of these $n$ numbers needs to be determined. Which of the following is TRUE about the number of comparisons needed?

  1. At least $2n-c$ comparisons, for some constant $c$ are needed.

  2. At most $1.5n-2$ comparisons are needed.

  3. At least $n\log_2 n$ comparisons are needed

  4. None of the above

in Algorithms
29.4k views

4 Comments

can u show me some cases where avg case is calculated like that ??
0
0

Similar Question asked again 

https://gateoverflow.in/1917/gate2014-1-39

1
1

Similar question was asked in GATE 2021:

https://gateoverflow.in/357450/gate-cse-2021-set-1-question-2

1
1

9 Answers

173 votes
173 votes
Best answer

An easier way to find it is by using Tournament Method Technique -

  1. To find the smallest element in the array will take $\mathbf{n-1}$ comparisons.
  2. To find the largest element - 
      a. After the first round of Tournament , there will be exactly  $\mathbf{n/2}$ numbers that will loose the round.
      b. So the biggest looser (the largest number) should be among these $50$ loosers.To find the largest number will take  $\mathbf{n/2 - 1}$.

Total Comparisons  $\mathbf{= (n-1) + (n/2 - 1) = 1.5n - 2}$.

Correct Answer: $B$

edited by

4 Comments

the first round of tournament will not require any comparison ?
1
1
edited by

Excellent explanation! Thanks!

Is  the "minimum number of comparisons required" = "maximum number of comparisons required", for finding the min and max elements among n elements?

From my understanding it seems so, please confirm if it is so or correct me if I am thinking the wrong way.

0
0
To find the smallest element in the array how will  it take n-1 comparisons
0
0
31 votes
31 votes

[need @arjun sir to verify it  ]

We are requested to find the MAX and MIN of the array of $n$ elements . This can be done as follws

Non Divide And Conquer

Max_Min(A,n,max,min)
  max=min=a[i]
  for i-> 2 to n
  {
      if A[i] > max   // First Comparision
         max=A[i]
      else if A[i] < min  // Seond Comparision
         min = A[i]
  }

Analysis

Best Case

When input is in increasing order

  • First Comparision : $n-1$ time
  • Second Comparision: $1$ time

Worst Case

When input is in Decreasing order

  • First Comparision : $n-1$ time
  • Second Comparision: $n-1$ time

Average Case

When First Comparision fails for half of the input

  • First Comparision : $n-1$ time
  • Second Comparision: $\frac{n}{2}$ time

 Divide And Conquer

Given a function to compute on $n$ inputs the divide-and-conquer strategy suggest splitting the inputs into $k$ distinct subsets, $1 < K \leq n$, yielding $k$ sub problems. These Sub problems must be solved, and then a method must be found to combine sub solutions into a solution of the whole.

Defining the Termination condition 'SMALL' of the problem , When $n \leq 2$. In this case, the maximum and minimum are $a[i] \ \text{if} \ n = 1$ . If $n = 2$, the problem can be solved by making one comparison.

How can we Combine The Subproblem ? If $\text{MAX(A)}$ and $\text{MIN(P)}$ are the maximum and minimum of the elements of A, then $\text{MAX(A)}$ is the larger of $\text{MAX(A1)}$ and $\text{MAX(A2)}$ Similarly for $\text{MIN(A)}$

 MaxMin(i, j, max, min)
// a[1:n] is a global array. Parameters i and j are integers,   
// 1≤i≤j≤n. The effect is to set max and min to the largest and  
// smallest values in a[i:j].
{
     if (i=j) then max := min := a[i]; //Small(P)
     else if (i=j-1) then // Another case of Small(P)
          {
                if (a[i] < a[j]) then max := a[j]; min := a[i];
                else max := a[i]; min := a[j];
          }
     else
     {
           // if P is not small, divide P into sub-problems.
           // Find where to split the set.
           mid := ( i + j )/2;
           // Solve the sub-problems.
           MaxMin( i, mid, max, min );
           MaxMin( mid+1, j, max1, min1 );
           // Combine the solutions.
           if (max < max1) then max := max1;
           if (min > min1) then min := min1;
     }
}

Analysis

Time Complexity

$T(n) =\begin{Bmatrix} 0 & n=1\\ 1 & n=2\\ 2T(\frac{n}{2})+2& n>2 \end{Bmatrix}$

Using Master Theorem  Case 1 follows . Hence $T(n)$ is $\Theta(n)$

Solution

$\begin{align*} \\ T(n)&=2T(\frac{n}{2})+2\\ &=4T(\frac{n}{4})+2^{2}+2 \\ & ... \\ & = 2^{k}T(\frac{n}{2^{k}})+\sum_{i=1}^{k}2^{i} & (\text{where}\sum_{i=1}^{k}2^{i}=2^{k+1}-2)\\ &=2^{k}T(\frac{n}{2^{k}})+2^{k+1}-2 & ( \text{where,} \ k=(\log n) -1) \\ &=2^{(\log n) -1}T(1)+2^{\log n} -2 \\ &=\frac{n}{2}+n-2 \\ &=\frac{3n}{2}-2 \end{align*}$

For Input Class : Numbers in Increasing Order Non-Divide And Conquer Strategy Perform Better than Divide And Conquer

Space Complexity

   $S(n) = n+ \log n + c $ Where $n$ is the input size and $\log n$ is the size of the stack . Hence the space complexity is $O(\log n)$

Example

Consider the array

$A[] = \begin{Bmatrix}
1 &2  &3  &4  & 5 &6  &7  &8  &9 \\
 22 & 13 & -5 &-8  &15  &60  &17  &31  &47
\end{Bmatrix}$

Video Example : https://www.youtube.com/watch?v=EHRL2LbS5LU

References

  1. https://www.youtube.com/watch?v=lEvzwEcjQ54&feature=youtu.be&t=1823
  2. Cormen
  3. http://somnathkayal.blogspot.in/2012/08/finding-maximum-and-minimum-using.html
by

4 Comments

@Puja you do not have to be irritated- you can just ignore and see what you need to see. I saw that this answer is taken from the given reference link -- can you post the same here as done in this answer? It was not a simple copy paste -- it was done with careful formatting and proper reference.
8
8
ok (y)
0
0
@pC Nptel video gave me complete clarity it was just awesome
0
0
26 votes
26 votes

There are N number: n1, n2, n3, n4, ...... N. 

We need to find the minimum and maximum element.

Naive approach: Assign the first number with min and max, and compare every other element linearly with these min and max variable, if we found another number is minimum/maximum, we will update our min and max variable according. Every number is compared twice. Total comparison = 2(n-1) = 2n - 2.

For every number, we are doing 2 comparisons, for every two numbers, we are doing 4 comparisons, Can we reduce this?

Better Algo: Pair all the elements.

(n1, n2) , (n3, n4), (n5, n6) ..........

When N is even, for the first pair, (n1, n2) compare them with each other and find min and max. Now, from every other pair, we first compare them, and whoever is minimum among them we compare it with min, and whoever is maximum among them will be compared with max. So for every other two elements, we have three comparisons.

Total comparison = (3* (n - 2) / 2 ) + 1 (for the first pair).

= (3*n / 2 ) - 3 + 1 = 1.5*n - 2.

When N is odd, Assign the first number with min and max, and now compare them with every other pair as mentioned above.

Total comparison = 3 * (n - 1) / 2 

Reference : Cormen

edited by
10 votes
10 votes
divide and conquer gives 1.5n - 2 comparison

3 Comments

didn't get it
0
0
edited by
what if two binary searches for min and max, total comparisons = 2logn

... My bad. The array isn't sorted, hence divide and conquer is the best approach.
0
0
Answer:

Related questions