with Ada.Calendar; with Ada.Integer_Text_IO; with Ada.Text_IO; use Ada.Calendar; use Ada.Integer_Text_IO; use Ada.Text_IO; -- ------------------------------------------------------------------- -- Main -- Purpose: -- Coordinate input, sorting, output, and elapsed-time reporting. -- Parameters: -- None. -- Returns: -- No value; runs the complete program. -- Side effects: -- Reads and writes files and prints elapsed-time results. -- ------------------------------------------------------------------- procedure Main is Max_Vals: constant Natural := 1000; type Integer_Array is array (Natural range <>) of Integer; Values: Integer_Array(0..Max_Vals); Count: Natural; Elapsed: Duration; -- ------------------------------------------------------------------- -- Read_File -- Purpose: -- Read integers from the named input file into Values and set Count. -- Parameters: -- fileSpec - path of the input file to read. -- Returns: -- No value; updates the shared Values array and Count. -- Side effects: -- Opens and closes the input file and prints read progress. -- ------------------------------------------------------------------- procedure Read_File(fileSpec: String) is Input_File: File_Type; begin Count := 0; put("READING INPUT FILE..."); Open(Input_File, In_File, fileSpec); While not End_Of_File(Input_File) loop Get(Input_File, Values(Count)); Count := Count + 1; exit when Count > Max_Vals; end loop; Close(Input_File); Put_Line("DONE."); end Read_File; -- ------------------------------------------------------------------- -- Save_File -- Purpose: -- Write the first Count values to the named output file. -- Parameters: -- fileSpec - path of the output file to create or replace. -- Returns: -- No value; writes the shared Values array to the file. -- Side effects: -- Creates and closes the output file and prints progress. -- ------------------------------------------------------------------- procedure Save_File(fileSpec: String) is Output_File: File_Type; begin put("SAVING OUTPUT FILE..."); Create(Output_File, Out_File, fileSpec); for Index in 0..Count-1 loop put(Output_File, Values(Index)); New_Line(Output_File); end loop; Close(Output_File); Put_Line("DONE."); end Save_File; -- ------------------------------------------------------------------- -- Merge -- Purpose: -- Combine two sorted arrays into one sorted array. -- Parameters: -- left - sorted first input array. -- right - sorted second input array. -- Returns: -- A new array containing every value from both inputs in order. -- Side effects: -- Creates a result array; the input arrays are not modified. -- ------------------------------------------------------------------- function Merge(left: Integer_Array; right: Integer_Array) return Integer_Array is result: Integer_Array(0 .. (left'Length + right'Length) - 1); lIDX: Natural := left'First; rIDX: Natural := right'First; sIDX: Natural := result'First; begin while lIDX <= left'Last and rIDX <= right'Last loop if left(lIDX) <= right(rIDX) then result(sIDX) := left(lIDX); lIDX := lIDX + 1; else result(sIDX) := right(rIDX); rIDX := rIDX + 1; end if; sIDX := sIDX + 1; end loop; while lIDX <= left'Last loop result(sIDX) := left(lIDX); lIDX := lIDX + 1; sIDX := sIDX + 1; end loop; while rIDX <= right'Last loop result(sIDX) := right(rIDX); rIDX := rIDX + 1; sIDX := sIDX + 1; end loop; return result; end Merge; -- ------------------------------------------------------------------- -- Merge_Sort -- Purpose: -- Recursively split an array and merge the sorted halves. -- Parameters: -- list - array of integers to sort. -- Returns: -- A new array containing the values from list in ascending order. -- Side effects: -- Creates sorted subarrays recursively; list is not modified. -- ------------------------------------------------------------------- function Merge_Sort(list: Integer_Array) return Integer_Array is middle: Integer; begin if list'Length <= 1 then return list; end if; middle := list'First + (list'Length/2) - 1; return Merge(Merge_Sort(list(list'First..middle)), Merge_Sort(list(middle + 1..list'Last))); end Merge_Sort; -- ------------------------------------------------------------------- -- Do_Sort -- Purpose: -- Time the in-place sort of the populated portion of Values. -- Parameters: -- None; uses the shared Values array and Count. -- Returns: -- Elapsed time in seconds. -- Side effects: -- Sorts the populated portion of Values in place and prints progress. -- ------------------------------------------------------------------- function Do_Sort return Duration is Start_Time, End_Time: Time; begin put("SORTING ELEMENTS..."); Start_Time := Clock; Values(0..Count - 1) := Merge_Sort(Values(0..Count - 1)); End_Time := Clock; Put_Line("DONE."); return End_Time - Start_Time; end Do_Sort; begin -- Run the complete file-read, sort, output, and statistics-reporting workflow. Put_Line("=========MERGESORT ADA=========="); Read_File("D:\Sorting\Sorting_Input.txt"); Elapsed := Do_Sort; Save_File("D:\Sorting\Output_Ada.txt"); Put_Line("==========RUN COMPLETE=========="); Put_Line("MERGESORT TOOK " & Duration'Image(Elapsed) & " SECONDS."); Put_Line("================================"); end Main;