Help on the following question? Code Review Section     Your co-worker has developed a function to find the median value in an unsorted array with an odd number of elements. For arrays with an even number of elements, this function does not apply.   To normally find such a value would be to sort the array and then obtain the value at the middle index of the array. For example, [5 2 53 1 4], the median is 4 because when it is sorted, the array will be [1 2 4 5 53] and the value at the middle index is 4.   Your co-worker’s function uses the partition function that is the same function as the one the QuickSort algorithm uses. Here’s the code he shows you:   public static int[] array = {6, 1, 20, 10, 4};   public static int findMedian(int lo, int hi, int medianIndex) {       int partitionIndex = partition(lo, hi);         if (medianIndex == partitionIndex)             return 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)); }   Upon reviewing the code, you find there is an error with it. Show your analysis by including the following:   Show what the current code will print on the screen and explain why it is wrong Show the update(s) to the code you would make to fix the problem

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter7: Arrays
Section7.1: One-dimensional Arrays
Problem 9E
icon
Related questions
Question

Help on the following question?

Code Review Section

 

 

Your co-worker has developed a function to find the median value in an unsorted array with an odd number of elements. For arrays with an even number of elements, this function does not apply.

 

To normally find such a value would be to sort the array and then obtain the value at the middle index of the array. For example, [5 2 53 1 4], the median is 4 because when it is sorted, the array will be [1 2 4 5 53] and the value at the middle index is 4.

 

Your co-worker’s function uses the partition function that is the same function as the one the QuickSort algorithm uses. Here’s the code he shows you:

 

public static int[] array = {6, 1, 20, 10, 4};

 

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

{

      int partitionIndex = partition(lo, hi);

 

      if (medianIndex == partitionIndex)

            return 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));

}

 

Upon reviewing the code, you find there is an error with it. Show your analysis by including the following:

 

  • Show what the current code will print on the screen and explain why it is wrong
  • Show the update(s) to the code you would make to fix the problem

 

 

Expert Solution
steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning