2023-08-14 19:52:24 +02:00
|
|
|
|
param([int]$numIntegers = 1000)
|
|
|
|
|
|
|
|
|
|
class BucketSort {
|
2023-08-14 18:19:23 +02:00
|
|
|
|
static Sort($targetList) {
|
|
|
|
|
|
|
|
|
|
$max = $targetList[0]
|
|
|
|
|
$min = $targetList[0]
|
|
|
|
|
|
|
|
|
|
for ($i = 1; $i -lt $targetList.Count; $i++) {
|
|
|
|
|
if ($targetList[$i] -gt $max) { $max = $targetList[$i] }
|
|
|
|
|
if ($targetList[$i] -lt $min) { $min = $targetList[$i]}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$holder = New-Object object[][] ($max - $min + 1)
|
|
|
|
|
|
|
|
|
|
for ($i = 0; $i -lt $holder.Count; $i++) {
|
|
|
|
|
$holder[$i] = @()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ($i = 0; $i -lt $targetList.Count; $i++) {
|
|
|
|
|
$holder[$targetList[$i] - $min]+=$targetList[$i]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$k = 0
|
|
|
|
|
|
|
|
|
|
for ($i = 0; $i -lt $holder.Count; $i++) {
|
|
|
|
|
if ($holder[$i].Count -gt 0) {
|
|
|
|
|
for ($j = 0; $j -lt $holder[$i].Count; $j++) {
|
|
|
|
|
$targetList[$k] = $holder[$i][$j]
|
|
|
|
|
$k++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
[BucketSort]::Sort($list)
|
2023-08-14 20:27:52 +02:00
|
|
|
|
[float]$elapsed = $stopWatch.Elapsed.TotalSeconds
|
|
|
|
|
"🕒 BucketSort of $numIntegers integers took $elapsed sec"
|
|
|
|
|
exit 0 # success
|