PowerShell/Scripts/measure-BubbleSort.ps1
Markus Fleschutz 499c86aaab Add measure-CountingSort.ps1, measure-InsertionSort.ps1,
measure-MergeSort.ps1, and measure-SelectionSort.ps1
2023-08-14 20:27:52 +02:00

24 lines
749 B
PowerShell

param([int]$numIntegers = 1000)
class BubbleSort {
static Sort($targetList) {
$n = $targetList.Count
for ($i = 0; $i -lt $n; $i+=1) {
for ($j = 0; $j -lt $n-1; $j+=1) {
if($targetList[$j] -gt $targetList[$j+1]) {
$temp = $targetList[$j+1]
$targetList[$j+1]=$targetList[$j]
$targetList[$j]=$temp
}
}
}
}
}
$list = (1..$NumIntegers | foreach{Get-Random -minimum 1 -maximum $numIntegers})
$stopWatch = [system.diagnostics.stopwatch]::startNew()
[BubbleSort]::Sort($list)
[float]$elapsed = $stopWatch.Elapsed.TotalSeconds
"🕒 BubbleSort of $numIntegers integers took $elapsed sec"
exit 0 # success