Question: How would i write the siftUp and siftDown method? I can attach my HeapSortApp class as well if needed. ` ` ` import java.util.Comparator; public

How would i write the siftUp and siftDown method? I can attach my HeapSortApp class as well if needed. ```
import java.util.Comparator;
public class SortAlgorithms {
public static void heapsort(T[] records, Comparator c){
for (int i=1; i0; k--){
T t = records[k];
records[k]= records[0];
records[0]= t;
}
siftDown(records, k, c);
printRecords("Sorted Array:", records);
}
private static void siftDown(T[] records, int k, Comparator c){
// TODO ADD CODE HERE
}
private static void siftUp(T[] records, int i, Comparator c){
// TODO ADD CODE HERE
}
private static void printRecords(String hdr, T[] records){
System.out.println(hdr);
for(int i=0; i ```
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SortApp {
|
public static void main(String[] args){
StudentRecord[] records = null;
try {
records = readRecordsFromFile(args[0]);
} catch (FileNotFoundException e){
System.err.println("Input file not found.");
return;
}
printRecords("Records read from input file:", records);
// sort the records using in place heapsort
SortingAlgorithms.heapsort(records, new StudentRecordComparator());
printRecords("Sorted records:", records);
}
private static StudentRecord[] readRecordsFromFile(String inFileName)
throws FileNotFoundException {
Scanner sc = new Scanner(new File(inFileName));
int nRecords; // number of records
int studentID;
String studentName;
StudentRecord[] records;
// read number of records using scanner
nRecords = sc.nextInt();
records = new StudentRecord[nRecords];
// read each record using scanner
for (int i=0; i
How would i write the siftUp and siftDown method?

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 Programming Questions!