Question: FinalList.java: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class FinalList { private final List unsortedList; public FinalList(String

 FinalList.java: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList;import java.util.LinkedList; import java.util.List; public class FinalList { private final List unsortedList;

FinalList.java:

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.List;

public class FinalList {

private final List unsortedList;

public FinalList(String filename) throws IOException {

BufferedReader in = new BufferedReader(new FileReader(filename));

unsortedList = new LinkedList();

while (in.ready()) {

String line = in.readLine();

int num = Integer.parseInt(line);

unsortedList.add(num);

}

in.close();

}

public String toString() {

return unsortedList.toString();

}

private List copyList() {

int n = unsortedList.size();

List list = new ArrayList(n);

for (int i = 0; i

list.add(unsortedList.get(i));

}

return list;

}

public List bubbleSort() {

List sortedList = copyList();

int n = sortedList.size();

boolean swapped;

do {

swapped = false;

for (int i = 1; i

if (sortedList.get(i - 1) > sortedList.get(i)) {

// swap

int temp = sortedList.get(i - 1);

sortedList.set(i - 1, sortedList.get(i));

sortedList.set(i, temp);

swapped = true;

}

}

--n;

} while (swapped);

return sortedList;

}

public List selectionSort() {

List sortedList = copyList();

int n = sortedList.size();

for (int j = 0; j

int i_Min = j;

for (int i = j + 1; i

if (sortedList.get(i)

i_Min = i;

}

}

if (i_Min != j) {

// swap

int temp = sortedList.get(j);

sortedList.set(j, sortedList.get(i_Min));

sortedList.set(i_Min, temp);

}

}

return sortedList;

}

public List heapSort() {

List sortedList = copyList();

Heap heap = new Heap(sortedList);

heap.heapify();

int end = heap.count() - 1;

while (end > 0) {

// swap

int temp = heap.get(end);

heap.set(end, heap.get(0));

heap.set(0, temp);

--end;

heap.siftDown(0, end);

}

return sortedList;

}

private class Heap {

private List a;

public Heap(List list) {

a = list;

}

public int count() {

return a.size();

}

public int get(int index) {

return a.get(index);

}

public void set(int index, int element) {

a.set(index, element);

}

public void heapify() {

int count = count();

int start = Math.floorDiv(count - 1, 2);

while (start >= 0) {

siftDown(start, count - 1);

--start;

}

}

public void siftDown(int start, int end) {

/* ADD CODE BELOW */

/* ADD CODE ABOVE */

}

}

public static void main(String[] args) throws IOException {

PrintWriter out = new PrintWriter("output.txt");

for (int i = 1; i

FinalList l = new FinalList("list" + i + ".txt");

out.println(l);

System.out.println("List " + i + ":");

// Bubble Sort

long start = System.nanoTime();

List sortedList = l.bubbleSort();

long end = System.nanoTime();

out.println(sortedList);

System.out.println("Bubble Sort execution time in seconds: " + (double)(end - start) / 1000000000L + " [first element = " + sortedList.get(0) + ", last element = " + sortedList.get(sortedList.size() - 1) + "]");

// Selection Sort

start = System.nanoTime();

sortedList = l.selectionSort();

end = System.nanoTime();

out.println(sortedList);

System.out.println("Selection Sort execution time in seconds: " + (double)(end - start) / 1000000000L + " [first element = " + sortedList.get(0) + ", last element = " + sortedList.get(sortedList.size() - 1) + "]");

// Heap Sort

start = System.nanoTime();

sortedList = l.heapSort();

end = System.nanoTime();

out.println(sortedList);

System.out.println("Heap Sort execution time in seconds: " + (double)(end - start) / 1000000000L + " [first element = " + sortedList.get(0) + ", last element = " + sortedList.get(sortedList.size() - 1) + "]");

out.println();

System.out.println();

}

out.close();

}

}

Implement the Heap Sort algorithm described here using the attached Java file: Final List java. Modify only the Heap.siftDown(...) method, which is marked with ADD CODE BELOW and ADD CODE ABOVE to complete the algorithm as described The given code iterates through three given input files list1.txt 2, list2.txt and list3.txt Ea; constructs each list and sorts them using Bubble Sort (given), Selection Sort (given), and Heap Sort (given but incomplete), and; outputs both to the console and to the file output.txt. An example console output is shown below (but note that the execution time depends on your machine): List 1: el Bubble Sort execution time in seconds 0.021210547 Cfirst ement 1, last element 2043] Selection Sort execution time in seconds 0.012048066 Cfirst el ement 1, last element 2043] Heap Sort execution time in seconds 0.003126217 [first el ement 1, last element 2043] List 2: Bubble Sort execution time in seconds 20.710765353 Cfirst e lement 3, last element 1310700 Selection Sort execution time in seconds. 5.976546974 Cfirst el ement 3, last element 131070] Heap Sort execution time in seconds 2.141787629 [first el ement 3, last element 131070] List 3: Bubble Sort execution time in seconds 89.854456986 first e lement 8, last element 524278] Selection Sort execution time in seconds 23.558998064 Cfirst e lement 8, last element 524278] Heap Sort execution time in seconds 7.547587415 [first el ement 8, last element 524278]

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!