Last active
August 21, 2022 15:24
-
-
Save SP3269/0e4a2025f08d188ed773c95a1006a8de to your computer and use it in GitHub Desktop.
Quicksort implementation in PowerShell. Written during a long meeting. Tested with ```quicksort (1..10000 | get-random -count 50)``` and the like
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function quicksort($in) { | |
| $n = $in.count | |
| switch ($n) { | |
| 0 {} | |
| 1 { $in[0] } | |
| 2 { if ($in[0] -lt $in[1]) {$in[0], $in[1]} else {$in[1], $in[0]} } | |
| default { | |
| $anchor = $in | get-random | |
| $lt = $in | ? {$_ -lt $anchor} | |
| $eq = $in | ? {$_ -eq $anchor} | |
| $gt = $in | ? {$_ -gt $anchor} | |
| @(quicksort $lt) + @($eq) + @(quicksort $gt) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment