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...
-
Suppose that, once a sunflower plant has started growing, the rate of growth at any time is proportional to the product of its height and the difference between its height at maturity and its current...
-
Define the legal terms precedent, res judicata, stare decisis, original jurisdiction, and appellate jurisdiction.
-
The director of a large public library must schedule employees to reshelve books and periodicals checked out of the library. The number of items checked out will determine the labor requirements. The...
-
A block placed under the head of the claw hammer as shown greatly facilitates the extraction of the nail. If a 47-lb pull on the handle is required to pull the nail, calculate the tension T in the...
-
Allie has bought a new apple orchard. The orchard has a single file of trees, numbered from 1 to N. Each tree has a certail number of ripe apples. Allie has a rule she wants to follow. She wants to...
-
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...
-
Epsilon plc had in issue 400,000 6% Debentures as at 31 March 2010 and on 31 December 2010 redeemed 300,000 of these debentures at 10% premium. Interest on debentures was paid annually in arrears on...
-
As the 1980s drew to a close, it is probably true to say that behavioural economics was on a firm footing and certainly a growing field. Briefly summarise developments since 1990 AND find a recent...
-
In the later year, prices are $1.50 per treat, $150 per dog house, and $7.50 per leash. What is nominal GDP? What is the GDP Deflator?
-
Provide a summary on political neutrality and impartiality, considering the various perspectives in political science or international relations?
-
12. A local farmer purchases $400 of supplies to grow potatoes which are then sold for $1000 to grocery stores and restaurants ($500 each). The grocery stores then sell their potatoes to consumers...
-
Explain inflation as development issue or problem that is happening in the context of the Philippines. Also, explain how this problem may affect you as well as the people in the Philippines. (200...
-
The Wren Construction Company reports its income by the completed contract method. At the end of 2014, the company completed a contract to construct a building at a total cost of $800,000. The...
-
Recall that Chapter 8 described the binary search algorithm for finding a particular entry in an ordered list. The idea behind binary search is to begin looking in the exact center of the list. If...
-
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...
-
1. Q: What is Docker? 2. Q: What is a data lake? 3. Q: What is a NoSQL database? 4. Q: What is a software development methodology? 5. Q: What is cross-platform development? 6. Q: What is Moore's Law?...
-
1. Q: What is virtual memory? 2. Q: What is a hash function? 3. Q: What is A/B testing? 4. Q: What is machine learning? 5. Q: What is a software patch? 6. Q: What is the difference between symmetric...
-
1. Q: What is RAID (Redundant Array of Independent Disks)? 2. Q: What is a digital signature? 3. Q: What is cloud storage? 4. Q: What is responsive web design? 5. Q: What is the difference between...
Study smarter with the SolutionInn App