Problem

The median of a collection of data is the middle value. One way to find the median is to s...

The median of a collection of data is the middle value. One way to find the median is to sort the data and take the value that is at—or nearly at—the center of the collection. But sorting does more than necessary to find the median. You need to find only the kth smallest entry in the collection for an appropriate value of k. To find the median of n items, you would take k as n/2 rounded up—that is,[n/2].

You can use the partitioning strategy of quick sort to find the kth smallest entry in an array. After choosing a pivot and forming the subarrays Smaller and Larger, as described in Segment 9.10, you can draw one of the following conclusions:

• If Smaller contains k or more entries, it must contain the kth smallest entry.

• If Smaller contains k - 1 entries, the kth smallest entry is the pivot.

• If Smaller contains fewer than k - 1 entries, the kth smallest entry is in Larger.

You now can develop a recursive solution to finding the kth smallest entry. The first and last conclusions correspond to the recursive calls. The remaining one is the base case.

Implement a recursive method that finds the kth smallest entry in an unsorted array. Use your method to find the median in the array.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 9
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT