Created
June 23, 2016 19:55
-
-
Save sergiors/ed439e0643263ebc19a35e5af7673c82 to your computer and use it in GitHub Desktop.
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
<?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