Question
what strategy could be implemented to improve the performance by applying one or more of principles to improve the efficiency of I/O public class MergeSort
what strategy could be implemented to improve the performance by applying one or more of principles to improve the efficiency of I/O
public class MergeSort { // The mergeSort method returns a sorted copy of the // String objects contained in the String array data. /** * Sorts the String objects using the merge sort algorithm. * * @param data the String objects to be sorted * @return the String objects sorted in ascending order */ public static String[] mergeSort(String[] data) {
if (data.length > 1) { String[] left = new String[data.length / 2]; String[] right = new String[data.length - left.length]; System.arraycopy(data, 0, left, 0, left.length); System.arraycopy(data, left.length, right, 0, right.length); left = mergeSort(left); right = mergeSort(right); return merge(left, right); } else { return data; } } /** * The merge method accepts two String arrays that are assumed * to be sorted in ascending order. The method will return a * sorted array of String objects containing all String objects * from the two input collections. * * @param left a sorted collection of String objects * @param right a sorted collection of String objects * @return a sorted collection of String objects */ public static String[] merge(String[] left, String[] right) { String[] data = new String[left.length + right.length]; int lIndex = 0; int rIndex = 0; for (int i=0; i --------- import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class Sort extends Thread{ /** * You are to implement this method. The method should invoke one or * more threads to read and sort the data from the collection of Files. * The method should return a sorted list of all of the String data * contained in the files. * * @param files * @return * @throws IOException * @throws InterruptedException */ File f; String sortData[] = new String[0]; public Sort(File f) { this.f = f; } public static String[] threadedSort(File[] files) throws IOException, InterruptedException { //throw new java.lang.IllegalStateException("Method not implemented"); Sort thread[] = new Sort[files.length]; int i; for(i =0;i String[] sortedData = new String[0]; for (File file : files) { String[] data = getData(file); data = MergeSort.mergeSort(data); sortedData = MergeSort.merge(sortedData, data); } return sortedData; } /** * This method will read in the string data from the specified * file and return the data as an array of String objects. * * @param file the file containing the String data * @return String array containing the String data * @throws IOException thrown if any errors occur reading the file */ private static String[] getData(File file) throws IOException { ArrayList // Read the data from the file until the end of file is reached while (true) { String line = in.readLine(); if (line == null) { // the end of file was reached break; } else { data.add(line); } } //Close the input stream and return the data in.close(); return data.toArray(new String[0]); } void sort(File f){ String[] data = null; try { data = getData(f); } catch (IOException e) { // TODO Auto-generated catch block } data = MergeSort.mergeSort(data); sortData = MergeSort.merge(sortData, data); } @Override public void run() { // TODO Auto-generated method stub sort(f); } } ------ import java.io.File; import java.io.IOException; /** * The class SortTest is used to test the threaded and non-threaded * sort methods. This program will call each method to sort the data * contained in the four test files. This program will then test the * results to ensure that the results are sorted in ascending order. * * Simply run this program to verify that you have correctly implemented * the threaded sort method. The program will not verify if your sort * uses threads, but will verify if your implementation correctly * sorted the data contained in the four files. * * There should be no reason to make modifications to this class. */ public class SortTest { public static void main(String[] args) throws IOException, InterruptedException { File[] files = {new File("enable1.txt"), new File("enable2k.txt"), new File("lower.txt"), new File("mixed.txt")}; // Run Sort.sort on the files long startTime = System.nanoTime(); String[] sortedData = Sort.sort(files); long stopTime = System.nanoTime(); double elapsedTime = (stopTime - startTime) / 1000000000.0; // Test to ensure the data is sorted for (int i=0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started