Skip to content

Instantly share code, notes, and snippets.

@sergiors
Created June 23, 2016 19:55
Show Gist options
  • Save sergiors/ed439e0643263ebc19a35e5af7673c82 to your computer and use it in GitHub Desktop.
Save sergiors/ed439e0643263ebc19a35e5af7673c82 to your computer and use it in GitHub Desktop.
<?php
function qsort(array $xs)
{
$xs = array_values($xs);
if (false === array_key_exists(1, $xs)) {
return $xs;
}
$pivot = $xs[0];
$xss = array_slice($xs, 1);
$left = array_filter($xss, function ($x) use ($pivot) {
return $x < $pivot;
});
$right = array_filter($xss, function ($x) use ($pivot) {
return $x >= $pivot;
});
return array_merge(qsort($left), [$pivot], qsort($right));
}
$numbers = [2, 1, 4, 100, 3, 99];
var_dump(qsort($numbers));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment