2023-08-14 19:52:24 +02:00
|
|
|
|
param([int]$numIntegers = 1000)
|
|
|
|
|
|
|
|
|
|
class BubbleSort {
|
2023-08-14 18:19:23 +02:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-14 20:27:52 +02:00
|
|
|
|
$list = (1..$NumIntegers | foreach{Get-Random -minimum 1 -maximum $numIntegers})
|
2023-08-14 18:19:23 +02:00
|
|
|
|
$stopWatch = [system.diagnostics.stopwatch]::startNew()
|
|
|
|
|
[BubbleSort]::Sort($list)
|
2023-08-14 20:27:52 +02:00
|
|
|
|
[float]$elapsed = $stopWatch.Elapsed.TotalSeconds
|
|
|
|
|
"🕒 BubbleSort of $numIntegers integers took $elapsed sec"
|
|
|
|
|
exit 0 # success
|