package MergeSort; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; public class mergeSort { // ------------------------------------------------------------------- // loadFile // Purpose: // Read non-blank integer lines from the named input file. // Parameters: // fileSpec - path of the text file to read. // Returns: // A list containing the parsed integers in input order. // Side effects: // Opens and closes the input file and prints read progress. // ------------------------------------------------------------------- static List loadFile(String fileSpec) throws IOException { System.out.print("READING INPUT FILE..."); List values = new ArrayList<>(); for(String line : Files.readAllLines(Path.of(fileSpec))) { if(!line.isBlank()) values.add(Integer.valueOf(line)); } System.out.println("DONE."); return values; } // ------------------------------------------------------------------- // saveFile // Purpose: // Write each integer in values to the named output file. // Parameters: // values - integer list to write in its current order. // fileSpec - path of the output file to create or replace. // Returns: // No value; propagates file errors as IOException. // Side effects: // Writes and closes the output file and prints progress. // ------------------------------------------------------------------- static void saveFile(List values, String fileSpec) throws IOException { System.out.print("READING INPUT FILE..."); Files.write(Path.of(fileSpec), values.stream().map(String::valueOf).toList()); System.out.println("DONE."); } // ------------------------------------------------------------------- // merge // Purpose: // Merge two sorted lists into one sorted list. // Parameters: // left - sorted first input list. // right - sorted second input list. // Returns: // A new list containing both inputs in ascending order. // Side effects: // Removes values from the input lists as it merges them. // ------------------------------------------------------------------- static List merge(List left, List right) { List result = new ArrayList<>(); while(left.size() > 0 && right.size() > 0) { if(left.get(0) <= right.get(0)) { result.add(left.get(0)); left.remove(0); } else { result.add(right.get(0)); right.remove(0); } } for(int l=0;l mergesort(List values) { if(values.size() <= 1) return values; List left = new ArrayList<>(); List right = new ArrayList<>(); int middle = (int)Math.floor(values.size() / 2); for(int i = 0; i < values.size();i++) { if(i < middle) left.add(values.get(i)); else right.add(values.get(i)); } left = mergesort(left); right=mergesort(right); return merge(left,right); } // ------------------------------------------------------------------- // doSort // Purpose: // Time the merge sort and write its sorted result. // Parameters: // values - list of integers to sort. // Returns: // Elapsed sorting time in seconds. // Side effects: // Prints progress and writes the sorted values to the output file. // ------------------------------------------------------------------- static Double doSort(List values) throws Exception { System.out.print("SORTING ELEMENTS..."); Long startTime = System.nanoTime(); values = mergesort(values); Long endTime = System.nanoTime(); System.out.println("DONE."); saveFile(values, "D:/Sorting/Output_JAVA.txt"); return (endTime - startTime) / 1_000_000_000.0; } // ------------------------------------------------------------------- // main // Purpose: // Run the input, sort, output, and timing-report workflow. // Parameters: // args - command-line arguments (unused). // Returns: // No value. // Side effects: // Reads and writes files and prints timing and completion messages. // ------------------------------------------------------------------- public static void main(String[] args) throws Exception { System.out.println("=========MERGESORT JAVA========="); List values = loadFile("D:/Sorting/Sorting_Input.txt"); Double elapsed = doSort(values); System.out.println("==========RUN COMPLETE=========="); System.out.printf("MERGESORT TOOK %.6f SECONDS.%n",elapsed); System.out.println("================================"); } }