mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-24 17:03:45 +01:00
97 lines
3.0 KiB
Markdown
97 lines
3.0 KiB
Markdown
|
*measure-SelectionSort.ps1*
|
||
|
================
|
||
|
|
||
|
This PowerShell script measures the speed of the SelectionSort algorithm.
|
||
|
SelectionSort is an in-place comparison sorting algorithm. It has an O(n2) time complexity,
|
||
|
which makes it inefficient on large lists, and generally performs worse than the similar
|
||
|
insertion sort. Selection sort is noted for its simplicity and has performance advantages
|
||
|
over more complicated algorithms in certain situations, particularly where auxiliary memory
|
||
|
is limited.
|
||
|
|
||
|
Parameters
|
||
|
----------
|
||
|
```powershell
|
||
|
PS> ./measure-SelectionSort.ps1 [[-numIntegers] <Int32>] [<CommonParameters>]
|
||
|
|
||
|
-numIntegers <Int32>
|
||
|
Specifies the number of integers to sort
|
||
|
|
||
|
Required? false
|
||
|
Position? 1
|
||
|
Default value 1000
|
||
|
Accept pipeline input? false
|
||
|
Accept wildcard characters? false
|
||
|
|
||
|
[<CommonParameters>]
|
||
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
||
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
||
|
```
|
||
|
|
||
|
Example
|
||
|
-------
|
||
|
```powershell
|
||
|
PS> ./measure-SelectionSort.ps1
|
||
|
🧭 Sorting 1000 integers by SelectionSort took 0.3351773 sec
|
||
|
|
||
|
```
|
||
|
|
||
|
Notes
|
||
|
-----
|
||
|
Author: Markus Fleschutz | License: CC0
|
||
|
|
||
|
Related Links
|
||
|
-------------
|
||
|
https://github.com/fleschutz/PowerShell
|
||
|
|
||
|
Script Content
|
||
|
--------------
|
||
|
```powershell
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Measures the speed of SelectionSort
|
||
|
.DESCRIPTION
|
||
|
This PowerShell script measures the speed of the SelectionSort algorithm.
|
||
|
SelectionSort is an in-place comparison sorting algorithm. It has an O(n2) time complexity,
|
||
|
which makes it inefficient on large lists, and generally performs worse than the similar
|
||
|
insertion sort. Selection sort is noted for its simplicity and has performance advantages
|
||
|
over more complicated algorithms in certain situations, particularly where auxiliary memory
|
||
|
is limited.
|
||
|
.PARAMETER numIntegers
|
||
|
Specifies the number of integers to sort
|
||
|
.EXAMPLE
|
||
|
PS> ./measure-SelectionSort.ps1
|
||
|
🧭 Sorting 1000 integers by SelectionSort took 0.3351773 sec
|
||
|
.LINK
|
||
|
https://github.com/fleschutz/PowerShell
|
||
|
.NOTES
|
||
|
Author: Markus Fleschutz | License: CC0
|
||
|
#>
|
||
|
|
||
|
param([int]$numIntegers = 1000)
|
||
|
|
||
|
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
|
||
|
"🧭 Sorting $numIntegers integers by SelectionSort took $elapsed sec"
|
||
|
exit 0 # success
|
||
|
```
|
||
|
|
||
|
*(generated by convert-ps2md.ps1 using the comment-based help of measure-SelectionSort.ps1 as of 09/01/2023 17:51:53)*
|