Sort the sequence 3, 1, 4, 1, 5, 9, 2, 6, 5 using insertion sort
Read moreTextbook Solutions for Data Structures and Algorithm Analysis in Java
Question
The following divide-and-conquer algorithm is proposed for finding the simultaneousmaximum and minimum: If there is one item, it is the maximum and minimum,and if there are two items, then compare them and in one comparison you can findthe maximum and minimum. Otherwise, split the input into two halves, dividedas evenly as possibly (if N is odd, one of the two halves will have one more elementthan the other). Recursively find the maximum and minimum of each half, andthen in two additional comparisons produce the maximum and minimum for theentire problem.a. Suppose N is a power of 2. What is the exact number of comparisons used bythis algorithm?b. Suppose N is of the form 3 2k. What is the exact number of comparisons usedby this algorithm?c. Modify the algorithm as follows: When N is even, but not divisible by four, splitthe input into sizes of N/2 1 and N/2 + 1. What is the exact number ofcomparisons used by this algorithm?
Solution
The first step in solving 7 problem number 40 trying to solve the problem we have to refer to the textbook question: The following divide-and-conquer algorithm is proposed for finding the simultaneousmaximum and minimum: If there is one item, it is the maximum and minimum,and if there are two items, then compare them and in one comparison you can findthe maximum and minimum. Otherwise, split the input into two halves, dividedas evenly as possibly (if N is odd, one of the two halves will have one more elementthan the other). Recursively find the maximum and minimum of each half, andthen in two additional comparisons produce the maximum and minimum for theentire problem.a. Suppose N is a power of 2. What is the exact number of comparisons used bythis algorithm?b. Suppose N is of the form 3 2k. What is the exact number of comparisons usedby this algorithm?c. Modify the algorithm as follows: When N is even, but not divisible by four, splitthe input into sizes of N/2 1 and N/2 + 1. What is the exact number ofcomparisons used by this algorithm?
From the textbook chapter Sorting you will find a few key concepts needed to solve this.
Visible to paid subscribers only
Step 3 of 7)Visible to paid subscribers only
full solution