Let be an array containing integers. Let be the lowest upper bound on the number of comparisons of the array elements, required to find the minimum and maximum values in an arbitrary array of elements. Which one of the following choices is correct?
View worked solution
Answer: C
To find both the minimum and maximum elements in an arbitrary array of size , we can use an optimal pairwise comparison algorithm:
- If is odd, initialize both
minandmaxto the first element. If is even, compare the first two elements, setminto the smaller one andmaxto the larger one. - Process the remaining elements in pairs. For each pair, compare the two elements with each other, then compare the smaller element with
minand the larger element withmax.
The total number of comparisons, , required by this optimal algorithm is:
- If is even: (1 comparison for the first pair, and comparisons for the remaining pairs).
- If is odd: ( comparisons for the pairs).
We can write this compactly as:
Let us analyze the bounds for when :
- Since , we have for all .
- Comparing with :
- For even : .
- For odd : .
Therefore, the correct choice is C: .
Free practice · Your answers on this page are not saved.
