Does this code show proper implementation of quick sort algorithm? If not, why? Can this code further be improved?
<?php
$unsorted = range(1,20);
shuffle($unsorted);
function quick_sort($array)
{
// find array size
$length = count($array);
// base case test, if array of length 0 then just return array
if($length <= 1){
return $array;
}
else{
// select random element as pivot
$rand = rand(0,$length-1);
$pivot = $array[$rand];
// create two partitions
$left = $right = array();
// place elements around pivot by comparison
for($i = 0; $i < count($array); $i++)
{
if($i==$rand)continue;
if($array[$i] < $pivot){
$left[] = $array[$i];
}
else{
$right[] = $array[$i];
}
}
// recurse
return array_merge(quick_sort($left), array($pivot), quick_sort($right));
}
}
echo '<pre>';
print_r($unsorted);
print_r(quick_sort($unsorted));
?>