Created
August 23, 2016 16:00
-
-
Save antoniopicone/25478344e0bfea6c734975d89fad7a17 to your computer and use it in GitHub Desktop.
A simple implementation of Quick Sort in PHP
This file contains 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 quickSort($array) { | |
if(sizeof($array) <= 1) return $array; | |
$pivot = $array[0]; | |
$left = $right = array(); | |
for($i=1;$i<sizeof($array);$i++) { | |
if($array[$i]<$pivot) { | |
$left[]= $array[$i]; | |
} | |
else { | |
$right[]= $array[$i]; | |
} | |
} | |
return array_merge( quickSort($left), array($pivot), quickSort($right) ); | |
} | |
$arr = array(3,5,1,2,8,7,6,9,4); | |
echo implode(',', quickSort($arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment