Rename to measure-*.ps1 and add measure-sorting-algorithms.ps1

This commit is contained in:
Markus Fleschutz 2023-08-14 19:52:24 +02:00
parent 12b56f4a3c
commit 33b9e684d9
5 changed files with 43 additions and 12 deletions

View File

@ -1,4 +1,6 @@
class BubbleSort {
param([int]$numIntegers = 1000)
class BubbleSort {
static Sort($targetList) {
$n = $targetList.Count
@ -15,7 +17,7 @@
}
$stopWatch = [system.diagnostics.stopwatch]::startNew()
$list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000})
$list = (1..$NumIntegers | %{Get-Random -Minimum 1 -Maximum 1000})
[BubbleSort]::Sort($list)
[float]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"🕒 BubbleSort of 1000 integers took $Elapsed sec"
"🕒 BubbleSort of $NumIntegers integers took $Elapsed sec"

View File

@ -1,4 +1,6 @@
class BucketSort {
param([int]$numIntegers = 1000)
class BucketSort {
static Sort($targetList) {
$max = $targetList[0]
@ -34,7 +36,7 @@
}
$stopWatch = [system.diagnostics.stopwatch]::startNew()
$list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000})
$list = (1..$numIntegers | %{Get-Random -Minimum 1 -Maximum 1000})
[BucketSort]::Sort($list)
[float]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"🕒 BucketSort of 1000 integers took $Elapsed sec"
"🕒 BucketSort of $numIntegers integers took $Elapsed sec"

View File

@ -1,4 +1,6 @@
class HeapSort {
param([int]$numIntegers = 1000)
class HeapSort {
static Sort($targetList) {
$heapSize = $targetList.Count
@ -43,7 +45,7 @@
}
$stopWatch = [system.diagnostics.stopwatch]::startNew()
$list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000})
$list = (1..$numIntegers | %{Get-Random -Minimum 1 -Maximum 1000})
[HeapSort]::Sort($list)
[float]$Elapsed = $StopWatch.Elapsed.TotalSeconds
"🕒 HeapSort of 1000 integers took $Elapsed sec"
"🕒 HeapSort of $numIntegers integers took $Elapsed sec"

View File

@ -1,4 +1,6 @@
class QuickSort {
param([int]$numIntegers = 1000)
class QuickSort {
static Sort($targetList, $left, $right) {
$i=$left
$j=$right
@ -24,7 +26,7 @@
}
$stopWatch = [system.diagnostics.stopwatch]::startNew()
$list = (1..1000 | ForEach{Get-Random -Minimum 1 -Maximum 1000})
$list = (1..$numIntegers | 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"
"🕒 QuickSort of $numIntegers integers took $Elapsed sec"

View File

@ -0,0 +1,23 @@
<#
.SYNOPSIS
Measure sorting algorithms
.DESCRIPTION
This PowerShell script measures the speed of several sorting algorithms and prints it.
.PARAMETER numIntegers
Specifies the number of integers to sort (3000 by default)
.EXAMPLE
PS> ./measure-sorting-algorithms.ps1
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([int]$numIntegers = 3000)
" "
& "$PSScriptRoot/measure-BubbleSort.ps1" $numIntegers
& "$PSScriptRoot/measure-BucketSort.ps1" $numIntegers
& "$PSScriptRoot/measure-HeapSort.ps1" $numIntegers
& "$PSScriptRoot/measure-QuickSort.ps1" $numIntegers
exit 0 # success