Question: Hi, I need some help with this. Thanks! The Code: package heapsort.template; import java.util.Random; import javax.swing.JOptionPane; public class HeapSortTemplate { public static void main(String[] args)

Hi, I need some help with this. Thanks!

Hi, I need some help with this. Thanks! The Code: package heapsort.template;

The Code:

package heapsort.template;

import java.util.Random; import javax.swing.JOptionPane;

public class HeapSortTemplate { public static void main(String[] args) { int size = Integer.parseInt(JOptionPane.showInputDialog("How Many Items to" + " sort," + " n = ?")); int[] data = new int [size]; randomValues(data); //int[] data = {7,40,36,17,3,25,1,2}; System.out.println("****** Unsorted *******"); for(int index = 0; index

//reheapDownward(8, data, 0); System.out.println("*********************");

heapSort(data); System.out.println("****** Sorted ******"); for(int index = 0; index = size) //base case, root has no children { return; } if(root*2+1 == size - 1) //one child, a left child { if(data[root] > data[root*2+1]) // base case 2, the root is > than it's child { return; } else // swap it with it's child { swap(data, root, root*2+1); return; } } //root has 2 children if(data[root] > data[root*2+1] && data[root] > data[root*2+2]) { return; // base case 3, root larger than both of children } else // swap root with its largest child { if(data[root*2+1] > data[root*2+2])// left child > than right { swap(data, root, root*2+1); reheapDownward(size, data, root*2+1); // build left subtree into a heap return; } else // root is smaller than right child { swap(data, root, root*2+2); reheapDownward(size, data, root*2+2); // build left subtree into a heap return; } } } public static void heapSort(int[] data) { // Step 1: do nothing, all arrays are left balanced binary trees by default // remember 2p+1, 2p+2 // Step 2: // build the initial heap for(int p = data.length/2-1; p >= 0; p--) //all root node in the tree { reheapDownward(data.length, data, p); } // Step 3: repeatedly place root in it's proper index, rebuild the heap ignoring it for(int i = 1; i Program 7 Heap Sort and Sorting Performance Complete the coding/testing of the heap sort method we began in class. Then modify your program so that it outputs a comparison of the actual number of swaps it performs to the predicted number of swaps, and a comparison of the actual sort effort to the predicted and minimum sort effort. The output should be formatted exactly as shown below. Actual swaps xxxx; Predicted swaps xxxx Actual sort effort xxxx: Predicted sort effort xxxx: Min sort effort xxxx As discussed in class, the minimum sort effort is nlog,n. The predicted sort effort is 3nlog,n, and the predicted number of swaps is two thirds of the predicted sort effort (see pg 457/458) To do this: modify the project class to include two static integer class level variables that will store the actual number of comparisons and actual number of swaps required to perform a sort modify the swap method to count the number of swaps by incrementing one of the class level variables. modify the reheapDownward method to keep track to the sort effort by incrementing one of the class level variables whenever a comparison is made. modify the main method to product the two lines of required output

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!