Imports System Imports System.Diagnostics Imports System.Collections.Generic Imports System.IO Module Program ' ------------------------------------------------------------------- ' LoadFile ' Purpose: ' Read non-empty integer lines from an input file. ' Parameters: ' fileSpec - path of the input 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. ' ------------------------------------------------------------------- Function LoadFile(fileSpec As String) As List(Of Integer) Console.Write("READING INPUT FILE...") Dim values As New List(Of Integer)() For Each line In File.ReadLines(fileSpec) If Not String.IsNullOrWhiteSpace(line) Then values.Add(Integer.Parse(line)) End If Next Console.WriteLine("DONE.") Return values End Function ' ------------------------------------------------------------------- ' SaveFile ' Purpose: ' Write each integer in values to a separate output-file line. ' Parameters: ' values - integers to write. ' fileSpec - path of the output file to create or replace. ' Returns: ' No value. ' Side effects: ' Creates or replaces the output file and prints progress. ' ------------------------------------------------------------------- Sub SaveFile(values As List(Of Integer), fileSpec As String) Console.Write("SAVING OUTPUT FILE...") File.WriteAllLines(fileSpec, values.ConvertAll(Function(value) value.ToString())) Console.WriteLine("DONE.") End Sub ' ------------------------------------------------------------------- ' 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 all values in ascending order. ' Side effects: ' Removes values from the input lists as it merges them. ' ------------------------------------------------------------------- Function Merge(left As List(Of Integer), right As List(Of Integer)) As List(Of Integer) Dim result As New List(Of Integer) While left.Count() > 0 And right.Count() > 0 If left(0) <= right(0) Then result.Add(left(0)) left.RemoveAt(0) Else result.Add(right(0)) right.RemoveAt(0) End If End While For l = 0 To left.Count() - 1 result.Add(left(l)) Next For r = 0 To right.Count() - 1 result.Add(right(r)) Next Return result End Function ' ------------------------------------------------------------------- ' MergeSort ' Purpose: ' Recursively divide a list, sort each half, and merge them. ' Parameters: ' values - integers to sort. ' Returns: ' A list containing the values in ascending order. ' Side effects: ' Creates sorted lists without modifying the input list. ' ------------------------------------------------------------------- Function MergeSort(values As List(Of Integer)) As List(Of Integer) If values.Count() <= 1 Then Return values End If Dim left As New List(Of Integer) Dim right As New List(Of Integer) Dim middle As Integer = Math.Floor(values.Count() / 2) For i = 0 To values.Count() - 1 If i < middle Then left.Add(values(i)) Else right.Add(values(i)) End If Next left = MergeSort(left) right = MergeSort(right) Return Merge(left, right) End Function ' ------------------------------------------------------------------- ' 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. ' ------------------------------------------------------------------- Function DoSort(values As List(Of Integer)) As Double Console.Write("SORTING ELEMENTS...") Dim timer = Stopwatch.StartNew() values = MergeSort(values) timer.Stop() Console.WriteLine("DONE.") SaveFile(values, "D:\Sorting\Output_VB_NET.txt") Return timer.Elapsed.TotalSeconds End Function ' ------------------------------------------------------------------- ' 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. ' ------------------------------------------------------------------- Sub Main(args As String()) Console.WriteLine("=======MERGESORT VB .NET========") Dim values As List(Of Integer) = LoadFile("D:\Sorting\Sorting_Input.txt") Dim elapsed As Double = DoSort(values) Console.WriteLine("==========RUN COMPLETE==========") Console.WriteLine("MERGESORT TOOK {0:F6} SECONDS.", elapsed) Console.WriteLine("================================") End Sub End Module