# ------------------------------------------------------------------- # Read-File # Purpose: # Read non-empty integer lines from an input file. # Parameters: # FileSpec - path of the input file to read. # Returns: # An object containing the parsed integers. # Side effects: # Opens and closes the input file and prints read progress. # ------------------------------------------------------------------- function Read-File { param([string] $FileSpec) Write-Host "READING INPUT FILE..." -NoNewline $values = [Collections.Generic.List[int]]::new() foreach ($line in [IO.File]::ReadLines($FileSpec)) { if(-not [string]::ISNullOrWhiteSpace($line)) { $values.Add([int]::Parse($line)) } } Write-Host "DONE." return [PSCustomObject]@{ Values = $values } } # ------------------------------------------------------------------- # Save-File # Purpose: # Write each integer in Values to its own 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. # ------------------------------------------------------------------- function Save-File { param( [Collections.Generic.List[int]] $Values, [string] $FileSpec ) Write-Host "WRITING OUTPUT FILE..." -NoNewline [IO.File]::WriteAllLines($FIleSpec, [string[]] $Values) Write-Host "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 all values in ascending order. # Side effects: # Removes values from the input lists as it merges them. # ------------------------------------------------------------------- function Merge { param( [System.Collections.Generic.List[int]] $Left, [System.Collections.Generic.List[int]] $Right ) $result = [System.Collections.Generic.List[int]]::new() while($Left.Count -gt 0 -and $Right.Count -gt 0) { if($Left[0] -le $Right[0]) { $result.Add($Left[0]) $Left.RemoveAt(0) } else { $result.Add($Right[0]) $Right.RemoveAt(0) } } for($l=0;$l -lt $Left.Count;$l++) { $result.Add($Left[$l]) } for($r=0;$r -lt $Right.Count;$r++) { $result.Add($Right[$r]) } return $result } # ------------------------------------------------------------------- # Merge-Sort # 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 Merge-Sort { param( [System.Collections.Generic.List[int]] $Values ) if($Values.Count -le 1) { return $Values } $left = [System.Collections.Generic.List[int]]::new() $right = [System.Collections.Generic.List[int]]::new() $middle = [Math]::Floor($Values.Count/2) for($i=0;$i -lt $Values.Count;$i++) { if($i -lt $middle) { $left.Add($Values[$i]) } else { $right.Add($Values[$i]) } } $leftSorted = Merge-Sort -Values $left $rightSorted = Merge-Sort -Values $right return Merge -Left $leftSorted -Right $rightSorted } # ------------------------------------------------------------------- # Do-Sort # Purpose: # Time the merge sort and save its sorted result. # Parameters: # Values - integers to sort. # Returns: # Elapsed sorting time in seconds. # Side effects: # Prints progress and writes the sorted values to the output file. # ------------------------------------------------------------------- function Do-Sort { param( [System.Collections.Generic.List[int]] $Values ) Write-Host "SORTING ELEMENTS..." -NoNewline $timer = [System.Diagnostics.Stopwatch]::StartNew() $sorted = Merge-Sort -Values $Values $timer.Stop() Write-Host "DONE." Save-File -Values $sorted -FileSpec "D:\Sorting\Output_PS.txt" return $timer.Elapsed.TotalSeconds } Write-Host "=======MERGESORT POWERSHELL=======" $wrapper = Read-File "D:\Sorting\Sorting_Input.txt" $values = $wrapper.Values $elapsed = Do-Sort -Values $values Write-Host "========== RUN COMPLETE ==========" Write-Host ("MERGESORT TOOK {0:F6} SECONDS." -f $elapsed) Write-Host "=================================="