Computer algorithm question:
(a), pretend a,b,c are in an array (in slots E[0], E[1], E[2]),
and have your algorithm return the INDEX (namely 0, 1, or 2) of the median.
Your algorithm should take care not to destroy the array.
(c) How many comparisons are necessary in the worst case to find the median of three numbers? Justify your answer. Optimality argument is required.
(e): The parameters of the problem have simplified things so that there are no ties, and your answers to the first four parts can assume that
this is the case. Assume for a moment that ties are allowed. If your (unwritten, and you do NOT have to write a new one) algorithm were
to discover that the first two entries you compared were equal to each other, what can you say about the median of the entries in this 3-element array?
Expert Answer
In the worst case finding the median of three numbers will take three comparisons.
two comparisons are required to find the first element in the sorted order(most minimum).The second element in the order will take one comparison in the worst case.
If two numbers are same out of three numbers, then median is one of that number.