Hi all. I was trying to implement a simple quicksort algo in php but my brain has frozen again. I cant see an error with the code i hope you can. Its an array[][] with array[][0] as a string and array[][1] as a numeric value
i am trying to simply sort the numeric values and switch the string fields with them too. if you had that algo in php that you get the k biggest numbers in an array without sorting that would be helpful too. well here is the code i hope its something obvious.
function Quick_sort($array,$start,$end)
{
$middle=$array[floor(($start+$end)/2)][1];
$i=$start;
$j=$end;
do
{
while ($array[$i][1]<$middle) $i++;
while ($array[$j][1]>$middle) $j--;
if ($i<=$j)
{
$temp[1]=$array[$i][1];
$temp[0]=$array[$i][0];
$array[$i][1]=$array[$j][1];
$array[$i][0]=$array[$j][0];
$array[$j][1]=$temp[1];//switch parameter value
$array[$j][0]=$temp[0];//switch parameter name
$i++; $j--;
}
} while ($i<=$j);
if ($end<$j) Quick_sort($array, $start, $j);
if ($i<$end) Quick_sort($array, $i, $end);
return $array;
}
i am trying to simply sort the numeric values and switch the string fields with them too. if you had that algo in php that you get the k biggest numbers in an array without sorting that would be helpful too. well here is the code i hope its something obvious.
function Quick_sort($array,$start,$end)
{
$middle=$array[floor(($start+$end)/2)][1];
$i=$start;
$j=$end;
do
{
while ($array[$i][1]<$middle) $i++;
while ($array[$j][1]>$middle) $j--;
if ($i<=$j)
{
$temp[1]=$array[$i][1];
$temp[0]=$array[$i][0];
$array[$i][1]=$array[$j][1];
$array[$i][0]=$array[$j][0];
$array[$j][1]=$temp[1];//switch parameter value
$array[$j][0]=$temp[0];//switch parameter name
$i++; $j--;
}
} while ($i<=$j);
if ($end<$j) Quick_sort($array, $start, $j);
if ($i<$end) Quick_sort($array, $i, $end);
return $array;
}