Skip to content

Instantly share code, notes, and snippets.

@antoniopicone
Created August 23, 2016 16:00
Show Gist options
  • Save antoniopicone/25478344e0bfea6c734975d89fad7a17 to your computer and use it in GitHub Desktop.
Save antoniopicone/25478344e0bfea6c734975d89fad7a17 to your computer and use it in GitHub Desktop.
A simple implementation of Quick Sort in PHP
<?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