mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-02-02 19:09:13 +01:00
Add measure-CountingSort.ps1, measure-InsertionSort.ps1,
measure-MergeSort.ps1, and measure-SelectionSort.ps1
This commit is contained in:
parent
33b9e684d9
commit
499c86aaab
@ -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
|
@ -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
|
33
Scripts/measure-CountingSort.ps1
Normal file
33
Scripts/measure-CountingSort.ps1
Normal file
@ -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
|
@ -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
|
29
Scripts/measure-InsertionSort.ps1
Normal file
29
Scripts/measure-InsertionSort.ps1
Normal file
@ -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
|
50
Scripts/measure-MergeSort.ps1
Normal file
50
Scripts/measure-MergeSort.ps1
Normal file
@ -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
|
@ -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
|
22
Scripts/measure-SelectionSort.ps1
Normal file
22
Scripts/measure-SelectionSort.ps1
Normal file
@ -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
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user