Skip to content

Measure-Selectionsort

Measures the speed of SelectionSort

param([int]$numIntegers = 1000)

class SelectionSort {
    static Sort($targetList) {
        $n = $targetList.count

        for ($i = 0; $i -lt $n; $i++) {
            for ($j = $i + 1; $j -lt $n; $j++) {
                if ($targetList[$j] -lt $targetList[$i]) {
                    $tmp = $targetList[$i]
                    $targetList[$i] = $targetList[$j]
                    $targetList[$j] = $tmp
                }
            }
        }
    }
}

$list = (1..$numIntegers | foreach{Get-Random -minimum 1 -maximum $numIntegers})
$stopWatch = [system.diagnostics.stopwatch]::startNew()
[SelectionSort]::Sort($list)
[float]$elapsed = $stopWatch.Elapsed.TotalSeconds
$elapsed3 = "{0:N3}" -f $elapsed # formatted to 3 decimal places
"?? $elapsed3 sec to sort $numIntegers integers by SelectionSort"
exit 0 # success

Specifies the number of integers to sort

An interactive directory of PowerShell scripts.