For any $N$-sized array, I would like to prove that the minimum number of comparisons to find both the max and min element simultaneously is Show \begin{align} \frac{3N}{2} - 2 && \text{N: even} \\ \frac{3(N-1)}{2} && \text{N: odd} \end{align} It's clear to me that the above number of comparisons can guarantee that the minimum and maximum can be found. For the even case, all you need to do is consider $N/2$ distinct pairs (by distinct pairs, I just mean that no 2 pairs have any of the same elements). For each pair $(a,b)$, you find $x = \max(a,b)$ and $y = \min(a,b)$. Then you compare $x$ with the running max and y with the running y. So for each pair, excluding the first pair, we require 3 comparisons. For the first pair, we don't need to compare $x$ with the running max and y with the running y because the running max and min can be set to $x$ and $y$, respectively, since it wasn't initialized already. Hence why we have $-2$ in the answer. For an odd $N$, the idea is the same, except the last element cannot be paired. So we have $(N-1)/2$ pairs, giving us $\frac{3(N-1)}{2} - 2$ comparisons, and the last element adds 2 comparisons, giving us $\frac{3(N-1)}{2}$. Okay, but I don't know how to show that you cannot do any better than the above. How can I prove that the above bounds are lower bounds? Improve Article Save Article Like Article Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons. Examples:
First of all, how do we return multiple values from a function? We can do it either using structures or pointers.
Maximum and minimum of an array using Linear search:
Below is the implementation of the above approach:
Output: Minimum element is 1 Maximum element is 3000Time Complexity: O(n) In this method, the total number of comparisons is 1 + 2(n-2) in the worst case and 1 + n – 2 in the best case. Maximum and minimum of an array using the tournament method:Divide the array into two parts and compare the maximums and minimums of the two parts to get the maximum and the minimum of the whole array.
Below is the implementation of the above approach:
OutputMinimum element is 1 Maximum element is 3000 Time Complexity: O(n) Total number of comparisons: let the number of comparisons be T(n). T(n) can be written as follows: If n is a power of 2, then we can write T(n) as: T(n) = 2T(n/2) + 2After solving the above recursion, we get T(n) = 3n/2 -2Thus, the approach does 3n/2 -2 comparisons if n is a power of 2. And it does more than 3n/2 -2 comparisons if n is not a power of 2. Maximum and minimum of an array by comparing in pairs:If n is odd then initialize min and max as the first element. If n is even then initialize min and max as minimum and maximum of the first two elements respectively. For the rest of the elements, pick them in pairs and compare their maximum and minimum with max and min respectively. Below is the implementation of the above approach:
Output: Minimum element is 1 Maximum element is 3000Time Complexity: O(n) Total number of comparisons: Different for even and odd n, see below: If n is odd: 3*(n-1)/2 If n is even: 1 Initial comparison for initializing min and max, and 3(n-2)/2 comparisons for rest of the elements = 1 + 3*(n-2)/2 = 3n/2 -2Second and third approaches make the equal number of comparisons when n is a power of 2. In general, method 3 seems to be the best.Please write comments if you find any bug in the above programs/algorithms or a better way to solve the same problem. |