Skip to content

Instantly share code, notes, and snippets.

@SP3269
Last active August 21, 2022 15:24
Show Gist options
  • Select an option

  • Save SP3269/0e4a2025f08d188ed773c95a1006a8de to your computer and use it in GitHub Desktop.

Select an option

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
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