Question: Problem 4. (12 marks) Describe an algorithm for finding both the minimum and maximum of n numbers using fewer than 3n/2 key comparisons. Hints: The
Problem 4. (12 marks) Describe an algorithm for finding both the minimum and maximum of n numbers using fewer than 3n/2 key comparisons. Hints: The case of n = 1 or 2 is trivial, at most 1 key comparision is needed. When n > 2, one can use a loop to examine each element after the first two and conduct two key comparisons to determine whether it is a new candidate minimum/maximum. This algorithm requires 1 + 2(n - 2) key comparisons, which does not satisfy the condition given in the problem. To reduce key comparisons, process each pair of elements (after the first two) using a loop and determine if the smaller of the pair is a new candidate minimum and if the larger one is a new candidate maximum. You need to handle both cases when n is even or odd. Here are what you need to do for this problem. Write a pseudocode for your algorithm, which must use exactly one loop (as sketched above, you don't need multiple loops). . Explain why your algorithm uses fewer than 3n/2 key comparisons. Write a loop invariant that your algorithm maintains and show the properties of Initialization, Main- tenance, and Termination (including both Termination #1 and Termination #2)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
