From 499c86aaab5fd7da9dfadf23ba9abed8431073f2 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Mon, 14 Aug 2023 20:27:52 +0200 Subject: [PATCH] Add measure-CountingSort.ps1, measure-InsertionSort.ps1, measure-MergeSort.ps1, and measure-SelectionSort.ps1 --- Scripts/measure-BubbleSort.ps1 | 7 ++-- Scripts/measure-BucketSort.ps1 | 7 ++-- Scripts/measure-CountingSort.ps1 | 33 +++++++++++++++++ Scripts/measure-HeapSort.ps1 | 7 ++-- Scripts/measure-InsertionSort.ps1 | 29 +++++++++++++++ Scripts/measure-MergeSort.ps1 | 50 ++++++++++++++++++++++++++ Scripts/measure-QuickSort.ps1 | 7 ++-- Scripts/measure-SelectionSort.ps1 | 22 ++++++++++++ Scripts/measure-sorting-algorithms.ps1 | 7 +++- 9 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 Scripts/measure-CountingSort.ps1 create mode 100644 Scripts/measure-InsertionSort.ps1 create mode 100644 Scripts/measure-MergeSort.ps1 create mode 100644 Scripts/measure-SelectionSort.ps1 diff --git a/Scripts/measure-BubbleSort.ps1 b/Scripts/measure-BubbleSort.ps1 index 66fef416..c84a31fb 100644 --- a/Scripts/measure-BubbleSort.ps1 +++ b/Scripts/measure-BubbleSort.ps1 @@ -16,8 +16,9 @@ class BubbleSort { } } +$list = (1..$NumIntegers | foreach{Get-Random -minimum 1 -maximum $numIntegers}) $stopWatch = [system.diagnostics.stopwatch]::startNew() -$list = (1..$NumIntegers | %{Get-Random -Minimum 1 -Maximum 1000}) [BubbleSort]::Sort($list) -[float]$Elapsed = $StopWatch.Elapsed.TotalSeconds -"๐Ÿ•’ BubbleSort of $NumIntegers integers took $Elapsed sec" +[float]$elapsed = $stopWatch.Elapsed.TotalSeconds +"๐Ÿ•’ BubbleSort of $numIntegers integers took $elapsed sec" +exit 0 # success \ No newline at end of file diff --git a/Scripts/measure-BucketSort.ps1 b/Scripts/measure-BucketSort.ps1 index f7a01a11..d68170e4 100644 --- a/Scripts/measure-BucketSort.ps1 +++ b/Scripts/measure-BucketSort.ps1 @@ -35,8 +35,9 @@ class BucketSort { } } +$list = (1..$numIntegers | foreach{Get-Random -minimum 1 -maximum $numIntegers}) $stopWatch = [system.diagnostics.stopwatch]::startNew() -$list = (1..$numIntegers | %{Get-Random -Minimum 1 -Maximum 1000}) [BucketSort]::Sort($list) -[float]$Elapsed = $StopWatch.Elapsed.TotalSeconds -"๐Ÿ•’ BucketSort of $numIntegers integers took $Elapsed sec" +[float]$elapsed = $stopWatch.Elapsed.TotalSeconds +"๐Ÿ•’ BucketSort of $numIntegers integers took $elapsed sec" +exit 0 # success \ No newline at end of file diff --git a/Scripts/measure-CountingSort.ps1 b/Scripts/measure-CountingSort.ps1 new file mode 100644 index 00000000..d773ccea --- /dev/null +++ b/Scripts/measure-CountingSort.ps1 @@ -0,0 +1,33 @@ +๏ปฟparam([int]$numIntegers = 1000) + +class CountingSort { + static Sort($targetList) { + $min = 0 + $max = 0 + + for ($counter = 0; $counter -lt $targetList.Count; $counter++) { + if ($targetList[$counter] -lt $min) { $min = $targetList[$counter] } + if ($targetList[$counter] -gt $max) { $max = $targetList[$counter] } + } + + $arrayBucket = New-Object int[] ($max - $min + 1) + + for ($counter = 0; $counter -lt $targetList.Count; $counter++) { + $arrayBucket[$targetList[$counter]]++; + } + + $lastPosition = 0 + for ($counter = 0; $counter -lt $arrayBucket.Count ; $counter++) { + for ($innerCounter = 0; $innerCounter -lt $arrayBucket[$counter]; $innerCounter++) { + $targetList[$lastPosition++] = $counter + } + } + } +} + +$list = (1..$numIntegers | foreach{Get-Random -minimum 1 -maximum $numIntegers}) +$stopWatch = [system.diagnostics.stopwatch]::startNew() +[CountingSort]::Sort($list) +[float]$elapsed = $stopWatch.Elapsed.TotalSeconds +"๐Ÿ•’ CountingSort of $numIntegers integers took $elapsed sec" +exit 0 # success \ No newline at end of file diff --git a/Scripts/measure-HeapSort.ps1 b/Scripts/measure-HeapSort.ps1 index 2edad4c8..09c391d2 100644 --- a/Scripts/measure-HeapSort.ps1 +++ b/Scripts/measure-HeapSort.ps1 @@ -44,8 +44,9 @@ class HeapSort { } } +$list = (1..$numIntegers | foreach{Get-Random -minimum 1 -maximum $numIntegers}) $stopWatch = [system.diagnostics.stopwatch]::startNew() -$list = (1..$numIntegers | %{Get-Random -Minimum 1 -Maximum 1000}) [HeapSort]::Sort($list) -[float]$Elapsed = $StopWatch.Elapsed.TotalSeconds -"๐Ÿ•’ HeapSort of $numIntegers integers took $Elapsed sec" +[float]$elapsed = $stopWatch.Elapsed.TotalSeconds +"๐Ÿ•’ HeapSort of $numIntegers integers took $elapsed sec" +exit 0 # success \ No newline at end of file diff --git a/Scripts/measure-InsertionSort.ps1 b/Scripts/measure-InsertionSort.ps1 new file mode 100644 index 00000000..ca88f0b9 --- /dev/null +++ b/Scripts/measure-InsertionSort.ps1 @@ -0,0 +1,29 @@ +๏ปฟparam([int]$numIntegers = 1000) + +class InsertionSort { + static Sort($targetList) { + $n = $targetList.count + + for ($i = 0; $i -lt $n - 1; $i++) { + $j = $i + 1 + + while ($j -gt 0) { + + if ($targetList[$j - 1] -gt $targetList[$j]) { + $temp = $targetList[$j - 1] + $targetList[$j - 1] = $targetList[$j] + $targetList[$j] = $temp + } + + $j-- + } + } + } +} + +$list = (1..$numIntegers | foreach{Get-Random -minimum 1 -maximum $numIntegers}) +$stopWatch = [system.diagnostics.stopwatch]::startNew() +[InsertionSort]::Sort($list) +[float]$elapsed = $stopWatch.Elapsed.TotalSeconds +"๐Ÿ•’ InsertionSort of $numIntegers integers took $elapsed sec" +exit 0 # success \ No newline at end of file diff --git a/Scripts/measure-MergeSort.ps1 b/Scripts/measure-MergeSort.ps1 new file mode 100644 index 00000000..6eb0bd60 --- /dev/null +++ b/Scripts/measure-MergeSort.ps1 @@ -0,0 +1,50 @@ +๏ปฟparam([int]$numIntegers = 1000) + +class MergeSort { + + static Merge($theArray, $tempArray, $leftPos, $rightPos, $rightEnd) { + $leftEnd = $rightPos - 1 + $tmpPos = $leftPos + $numElements = $rightEnd - $leftPos + 1 + + while (($leftPos -le $leftEnd) -and ($rightPos -le $rightEnd)) { + if ($theArray[$leftPos].CompareTo($theArray[$rightPos]) -le 0) { + $tempArray[$tmpPos++] = $theArray[$leftPos++] + } + else { + $tempArray[$tmpPos++] = $theArray[$rightPos++] + } + } + + while ($leftPos -le $leftEnd) { $tempArray[$tmpPos++] = $theArray[$leftPos++] } + while ($rightPos -le $rightEnd) { $tempArray[$tmpPos++] = $theArray[$rightPos++] } + + for ($i = 0; $i -lt $numElements; $i++, $rightEnd--) { + $theArray[$rightEnd] = $tempArray[$rightEnd] + } + } + + static Sort($theArray) { + $tempArray = New-Object Object[] $theArray.Count + [MergeSort]::Sort($theArray, $tempArray, 0, ($theArray.Count - 1)) + } + + static Sort($theArray, $tempArray, $left, $right) { + if ($left -lt $right) { + + $center = [Math]::Floor(($left + $right) / 2) + + [MergeSort]::Sort($theArray, $tempArray, $left, $center) + [MergeSort]::Sort($theArray, $tempArray, ($center + 1), $right) + + [MergeSort]::Merge($theArray, $tempArray, $left, ($center + 1), $right) + } + } +} + +$list = (1..$numIntegers | foreach{Get-Random -minimum 1 -maximum $numIntegers}) +$stopWatch = [system.diagnostics.stopwatch]::startNew() +[MergeSort]::Sort($list) +[float]$elapsed = $stopWatch.Elapsed.TotalSeconds +"๐Ÿ•’ MergeSort of $numIntegers integers took $elapsed sec" +exit 0 # success diff --git a/Scripts/measure-QuickSort.ps1 b/Scripts/measure-QuickSort.ps1 index 54b3a8e0..7578b6af 100644 --- a/Scripts/measure-QuickSort.ps1 +++ b/Scripts/measure-QuickSort.ps1 @@ -25,8 +25,9 @@ class QuickSort { } } +$list = (1..$numIntegers | foreach{Get-Random -minimum 1 -maximum $numIntegers}) $stopWatch = [system.diagnostics.stopwatch]::startNew() -$list = (1..$numIntegers | ForEach{Get-Random -Minimum 1 -Maximum 1000}) [QuickSort]::Sort($list, 0, $list.Count-1) -[float]$Elapsed = $StopWatch.Elapsed.TotalSeconds -"๐Ÿ•’ QuickSort of $numIntegers integers took $Elapsed sec" +[float]$elapsed = $stopWatch.Elapsed.TotalSeconds +"๐Ÿ•’ QuickSort of $numIntegers integers took $elapsed sec" +exit 0 # success \ No newline at end of file diff --git a/Scripts/measure-SelectionSort.ps1 b/Scripts/measure-SelectionSort.ps1 new file mode 100644 index 00000000..1489fbbd --- /dev/null +++ b/Scripts/measure-SelectionSort.ps1 @@ -0,0 +1,22 @@ +๏ปฟ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 +"๐Ÿ•’ SelectionSort of $numIntegers integers took $elapsed sec" +exit 0 # success \ No newline at end of file diff --git a/Scripts/measure-sorting-algorithms.ps1 b/Scripts/measure-sorting-algorithms.ps1 index bf49354f..a120f29a 100644 --- a/Scripts/measure-sorting-algorithms.ps1 +++ b/Scripts/measure-sorting-algorithms.ps1 @@ -7,6 +7,8 @@ Specifies the number of integers to sort (3000 by default) .EXAMPLE PS> ./measure-sorting-algorithms.ps1 + ๐Ÿ•’ BubbleSort of 3000 integers took 6.041561 sec + ... .LINK https://github.com/fleschutz/PowerShell .NOTES @@ -14,10 +16,13 @@ #> param([int]$numIntegers = 3000) - " " & "$PSScriptRoot/measure-BubbleSort.ps1" $numIntegers & "$PSScriptRoot/measure-BucketSort.ps1" $numIntegers +& "$PSScriptRoot/measure-CountingSort.ps1" $numIntegers & "$PSScriptRoot/measure-HeapSort.ps1" $numIntegers +& "$PSScriptRoot/measure-InsertionSort.ps1" $numIntegers +& "$PSScriptRoot/measure-MergeSort.ps1" $numIntegers & "$PSScriptRoot/measure-QuickSort.ps1" $numIntegers +& "$PSScriptRoot/measure-SelectionSort.ps1" $numIntegers exit 0 # success