Question: There is something wrong with the findMedian Java function below. This one is designed to only deal with an odd number of array elements. The

There is something wrong with the findMedian Java function below. This one is designed to only deal with an odd number of array elements. The median value of an array is the element value of the middle index of the array when it is sorted. For example, {5, 2, 53, 1, 4}, the median is 4 because when it is sorted, the middle index has an element value of 4.

In this exercise, the code uses the partitionIt function which is the same function the quickSort algorithm uses. After reviewing the code, answer the following questions:

1) What is the expected output of this particular program?

2) What changes to the code are needed to fix the issue(s)?

public static int[] array = {5, 2, 53, 10, 4};

public static int findMedian(int lo, int hi, int medianIndex)

{ int partitionIndex = partitionIt(lo, hi); if (medianIndex == partitionIndex) return array[partitionIndex]; if (medianIndex < partitionIndex) return findMedian(partitionIndex + 1, hi, medianIndex); else return findMedian(lo, partitionIndex - 1, medianIndex); } public static void main (String args[]) { System.out.println(findMedian(0, 4, 2)); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!