Add test-BubbleSort.ps1, test-BucketSort.ps1, test-HeapSort.ps1, and

test-QuickSort.ps1
This commit is contained in:
Markus Fleschutz 2023-08-14 18:19:23 +02:00
parent 0084431270
commit 12b56f4a3c
4 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,21 @@
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
}
}
}
}
}
$stopWatch = [system.diagnostics.stopwatch]::startNew()
$list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000})
[BubbleSort]::Sort($list)
[float]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"🕒 BubbleSort of 1000 integers took $Elapsed sec"

View File

@ -0,0 +1,40 @@
class BucketSort {
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++
}
}
}
}
}
$stopWatch = [system.diagnostics.stopwatch]::startNew()
$list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000})
[BucketSort]::Sort($list)
[float]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"🕒 BucketSort of 1000 integers took $Elapsed sec"

49
Scripts/test-HeapSort.ps1 Normal file
View File

@ -0,0 +1,49 @@
class HeapSort {
static Sort($targetList) {
$heapSize = $targetList.Count
for ([int]$p = ($heapSize - 1) / 2; $p -ge 0; $p--) {
[HeapSort]::MaxHeapify($targetList, $heapSize, $p)
}
for ($i = $targetList.Count - 1; $i -gt 0; $i--) {
$temp = $targetList[$i]
$targetList[$i] = $targetList[0]
$targetList[0] = $temp
$heapSize--
[HeapSort]::MaxHeapify($targetList, $heapSize, 0)
}
}
static MaxHeapify($targetList, $heapSize, $index) {
$left = ($index + 1) * 2 - 1
$right = ($index + 1) * 2
$largest = 0
if ($left -lt $heapSize -and $targetList[$left] -gt $targetList[$index]) {
$largest = $left
}
else {
$largest = $index
}
if ($right -lt $heapSize -and $targetList[$right] -gt $targetList[$largest]) {
$largest = $right
}
if ($largest -ne $index) {
$temp = $targetList[$index]
$targetList[$index] = $targetList[$largest]
$targetList[$largest] = $temp
[HeapSort]::MaxHeapify($targetList, $heapSize, $largest)
}
}
}
$stopWatch = [system.diagnostics.stopwatch]::startNew()
$list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000})
[HeapSort]::Sort($list)
[float]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"🕒 HeapSort of 1000 integers took $Elapsed sec"

View File

@ -0,0 +1,30 @@
class QuickSort {
static Sort($targetList, $left, $right) {
$i=$left
$j=$right
$pivot = $targetList[($left+$right)/2]
while($i -le $j) {
while($targetList[$i] -lt $pivot -and $i -lt $right) {$i++}
while($targetList[$j] -gt $pivot -and $j -gt $left) {$j--}
if($i -le $j) {
$tmp = $targetList[$i]
$targetList[$i]=$targetList[$j]
$targetList[$j]=$tmp
$i++
$j--
}
}
if($left -lt $j) {[QuickSort]::Sort($targetList, $left, $j)}
if($i -lt $right) {[QuickSort]::Sort($targetList, $i, $right)}
}
}
$stopWatch = [system.diagnostics.stopwatch]::startNew()
$list = (1..1000 | ForEach{Get-Random -Minimum 1 -Maximum 1000})
[QuickSort]::Sort($list, 0, $list.Count-1)
[float]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"🕒 QuickSort of 1000 integers took $Elapsed sec"