The discussion shows our algorithm for building the initial heap in the heapsort algorithm. The algorithm is
Question:
The discussion shows our algorithm for building the initial heap in the heapsort algorithm. The algorithm is reasonably efficient, but we can make it even more efficient. The more efficient algorithm uses a method that creates a heap from a subtree of the complete binary tree. This method has the following specification:
You can write the heapifySubtree method yourself. Using this method, you can make an entire n-element array into a heap with the algorithm:
for (i = (n/2); i > 0; i--)
heapifySubtree(data, i-1, n);
For example, with n = 10, we will end up making the following sequence of activations:
heapifySubtree(data, 4, 10);
heapifySubtree(data, 3, 10);
heapifySubtree(data, 2, 10);
heapifySubtree(data, 1, 10);
heapifySubtree(data, 0, 10);
It turns out that this method is actually O(n) rather than O(n log n).
For this project, reimplement makeHeap, as outlined above.
private static void heapifySubtree( int[ ] data, int root0fSubtree, int n // Precondition: data is an array with at least // n elements, and rootOfSubtree < n. We will // consider data to represent a complete // binary tree, and in this representation the // node at data[rootOfSubtree] is the root of // a subtree called s. This subtree s is already // a heap, except that its root might be out of // place. // Postcondition: The subtree s has been // rearranged so that it is now a valid heap.
Step by Step Answer:
Here is an implementation of the heapifySubtree method in Java public void heapifySu...View the full answer
Students also viewed these Computer science questions
-
The R&D division of Simplex Chemical Corp. has just developed a chemical to sterilize the voracious mountain pine beetles that are invading Western Canada's forests. The president of Simplex is...
-
You are given a full complete binary tree T of height d (Figure 1). In such a tree the d-th layer consists of 24-1 leaves, and each of the vertices in the first (d 1) layers has exactly 2 children....
-
A compare-exchange operation on two array elements A[i] and A[j], where i < j, has the form COMPARE-EXCHANGE (A, i, j) 1 If A[i] > A[j] 2 exchange A[i] with A[j] After the compare-exchange operation,...
-
Assume that when human resource managers are randomly selected, 57% say job applicants should follow up within two weeks. If 9 human resource managers are randomly selected, find the probability that...
-
Refer to the TEMPAGE column in Data Set 18 in Appendix B, and construct a frequency table of the ages of patients admitted for voluntary surgery. Start at age 13 and use a class width of 17.5. Does...
-
Comment on the following article:s 1. https://www.apricitas.io/p/supply-demand-and-todays-inflation?utm_source=substack&utm_medium=email (EC) 2....
-
Life insurance policies in force. The table below represents all life insurance policies (in millions) in force on the lives of US residents for the years 1990 through 2018. Year No. of Policies (in...
-
A sample of 16 items provides a sample standard deviation of 9.5. Test the following hypotheses using = .05. What is your conclusion? Use both the p-value approach and the critical value approach....
-
if too small zoom in web page Operating exposure. Copy-Cat, Inc. has signed a deal to make vintage Nissan 240-Z sports cars for the next three years. The company will build the cars in Japan and ship...
-
On January 1, 2015, 100% of the outstanding stock of Solo Company was purchased by Plato Corporation for $3,300,000. At that time, the book value of Solo's net assets equaled $3,000,000. The excess...
-
Rewrite quicksort so that there are no recursive calls. The technique is to use a stack to keep track of which portions of the array still need to be sorted. Whenever we identify a portion of the...
-
On problem with mergesort is that it constantly needs a new temporary array. One solution to the problem is to rewrite the mergesort according to the following specification: void mergesort( int...
-
A 40-question multiple-choice exam is sometimes administered in lower-level statistics courses. The exam has a peculiar feature: 10 of the questions have two options, 13 have three options, 13 have...
-
Sketch plane / intersecting plane K. Then draw a line & in plane J that intersects plane Kat a single point. A X C B D E
-
Use a graphing utility to verify any five of the graphs that you drew by hand in Exercises 126. Data from exercise 1-26 1. x + 2y = 8 3. x2y> 10 2. 3x6y 12 4. 2xy > 4
-
The following information pertains to Porter Company for 2011. Ending inventory consisted of 30 units. Porter sold 320 units at \(\$ 30\) each. All purchases and sales were made with cash. Required...
-
In Problems 7780, use a numerical integration routine on a graphing calculator to find the area bounded by the graphs of the indicated equations over the given interval (when stated). Compute answers...
-
Solar Heating, Inc., had the following transactions for 2011: Required a. Determine the quantity and dollar amount of inventory at the end of the year, assuming Solar Heating Inc. uses the FIFO cost...
-
Evaluate each of the following. (1500/200) 0.5 - 1
-
Use this circle graph to answer following Exercises. 1. What fraction of areas maintained by the National Park Service are designated as National Recreation Areas? 2. What fraction of areas...
-
Section 3.3 presents basic operation and possible implementations of multipliers. A basic unit of such implementations is a shift - and-add unit. Show a Verilog implementation for this unit. Show how...
-
Repeat Exercise B.22, but for an unsigned divider rather than a multiplier. Data from in Repeat Exercise B.22 Section 3.3 presents basic operation and possible implementations of multipliers. A basic...
-
The ALU supported set on less than (slt) using just the sign bit of the adder. Lets try a set on less than operation using the values -7 ten and 6 ten . To make it simpler to follow the example, lets...
-
Berbice Inc. has a new project, and you were recruitment to perform their sensitivity analysis based on the estimates of done by their engineering department (there are no taxes): Pessimistic Most...
-
#3) Seven years ago, Crane Corporation issued 20-year bonds that had a $1,000 face value, paid interest annually, and had a coupon rate of 8 percent. If the market rate of interest is 4.0 percent...
-
I have a portfolio of two stocks. The weights are 60% and 40% respectively, the volatilities are both 20%, while the correlation of returns is 100%. The volatility of my portfolio is A. 4% B. 14.4%...
Study smarter with the SolutionInn App