Question: Please write this program in to generic form // File: Heapsort.java // A Java application to illustrate the use of a heapsort // Additional javadoc

Please write this program in to generic form

// File: Heapsort.java // A Java application to illustrate the use of a heapsort // Additional javadoc documentation is available at: // http://www.cs.colorado.edu/~main/docs/Heapsort.html /****************************************************************************** * * @version * Dec 18, 2017 ******************************************************************************/ /* CSC103 complete this conversion to generic */ public class Heapsort /* CSC103 end conversion region */ { /** * The main method illustrates the use of a heapsort to sort a * small array. **/ public static void main(String[ ] args) { // sample data array Student[] stds = new Student[] { new Student("Jeffrey", "Goldblum", 189, 18, 3.78), new Student("Malcolm", "McDowell", 193, 19, 1.75), new Student("Margaret", "Ryan", 123, 20, 3.9), new Student("Thomas", "Hanks", 130, 20, 3.2), new Student("Eugene", "Hackman", 96, 24, 2.0), new Student("William", "Smith", 202, 21, 2.5), new Student("Margaret", "Colin", 129, 18, 3.95) };

// Print the array before sorting: System.out.println("Here is the original student order:"); for (int i = 0; i < stds.length; i++) System.out.print(stds[i]); System.out.println( );

// sort by name Student.SetSortMode(stds, Student.SORT_NAME); heapsort(stds, stds.length); System.out.println("Sorting by name:"); for (int i = 0; i < stds.length; i++) System.out.print(stds[i]); System.out.println( );

// sort by id Student.SetSortMode(stds, Student.SORT_ID); heapsort(stds, stds.length); System.out.println("Sorting by student id:"); for (int i = 0; i < stds.length; i++) System.out.print(stds[i]); System.out.println( );

// sort by age Student.SetSortMode(stds, Student.SORT_AGE); heapsort(stds, stds.length); System.out.println("Sorting by age:"); for (int i = 0; i < stds.length; i++) System.out.print(stds[i]); System.out.println( );

// sort by gpa Student.SetSortMode(stds, Student.SORT_GPA); heapsort(stds, stds.length); System.out.println("Sorting by gpa:"); for (int i = 0; i < stds.length; i++) System.out.print(stds[i]); System.out.println( ); } /** * @param data * the array to be sorted * @param n * the number of elements to sort, (from data[0] * through data[n-1]) *

Precondition:
* data has at least n elements. *
Postcondition:
* If n is zero or negative then no work is done. Otherwise, * the elements of data have been rearranged so that * data[0] <= data[1] <= ... <= data[n-1]. * @exception ArrayIndexOutOfBoundsException * Indicates that data has fewer than n elements. * */ /* CSC103 complete this conversion - hint: public static ... */ public static void heapsort(int[ ] data, int n) { int temp; // Used during the swapping of two array locations /* CSC103 end conversion region */ int unsorted; // Size of the unsorted part of the array

makeHeap(data, n);

unsorted = n;

while (unsorted > 1) { unsorted--;

// Swap the largest element (data[0]) with the final element of unsorted part temp = data[0]; data[0] = data[unsorted]; data[unsorted] = temp;

reheapifyDown(data, unsorted); } } /* CSC103 complete this conversion */ private static void makeHeap(int[ ] data, int n) /* CSC103 end conversion region */

// Precondition: data is an array with at least n elements. // Postcondition: The elements of data have been rearranged so that the // complete binary tree represented by this array is a heap. { for (int k=1; k

data[p] = data[c]; data[c] = tmp; return p; } private static int parent(int i) { return (i-1)/2; } private static int lchild(int i) { return 2*i+1; } private static int rchild(int i) { return 2*i+2; } private static boolean leftExists(int nNodes, int k) { return lchild(k) <= (nNodes-1); } private static boolean rightExists(int nNodes, int k) { return rchild(k) <= (nNodes-1); }

/* CSC103 complete this conversion */ private static void reheapifyDown(int[ ] data, int n) /* CSC103 end conversion region */

// Precondition: data is an array with at least n elements. // Postcondition: The elements of data have been rearranged so that the // complete binary tree represente> void reheapifyDown(T[ ] data, int n) // Precondition: n > 0, and data is an array with at least n elements. These // elements form a heap, except that data[0] may be in an incorrect // location. // Postcondition: The data values have been rearranged so that the first // n elements of data now form a heap. { int current; int bigChildIndex; boolean heapOkay; current = 0; heapOkay = false; int cmp; while((!heapOkay) && leftExists(n, current)) { bigChildIndex = lchild(current); if (rightExists(n,current)) { /* CSC103 complete this conversion */ cmp = data[lchild(current)] <= data[rchild(current)] ? -1 : 1; /* CSC103 end conversion region */ bigChildIndex = cmp > 0 ? lchild(current) : rchild(current); } cmp = data[current].compareTo(data[bigChildIndex]); if (cmp < 0) { swap(data,current,bigChildIndex); current = bigChildIndex; } else heapOkay = true; } } }

class Student implements Comparable { static public final int SORT_ID = 0; static public final int SORT_NAME = 1; static public final int SORT_GPA = 2; static public final int SORT_AGE = 3; String firstName; String lastName; Integer age; Integer id; Double gpa; int sortMode = SORT_NAME; /* CSC103 - make sure you understand what this is doin */ public static void SetSortMode(Student[] ary, int mode) { for (int k=0; k

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!