Jump to content

QuickSort HELP PLEASE


yevo

Recommended Posts

I wrote this code to do a Quicksort of some data Can someone please help me with two areas. I have remarked what i need to write i just cant figure out how

 

 

if($size >= 3) {

// update keys to use start/end                        <-------

$args = array()

0=>$array[0],

floor($size/2) => $array[floor($size/2)],

$size-1=>$array[$size-1]

);

 

asort($args);

$args = array_keys($args);

$ret = $args[1];

} else {

// use rand with start and end      <------

$ret = array_rand($array);

}

 

return $ret;

}

 

 

function partition(&$array, $start, $end, $pivotIndex) {

swap($array,$pivotIndex,$end);

$storeIndex = $start;

 

for ($i=$start; $i < $end; $i++); {

if ($array[$i] < $pivotValue) {

swap($array,$i,$storeIndex);

$storeIndex = $storeIndex + 1;

}

 

swap($array,$storeIndex,$end);

}

 

return $storeIndex;

}

 

function swap(&$array,$left,$right) {

$temp = $array[$left];

$array[$left] = $array[$right];

$array[$right] = $temp;

}

 

$do = quicksort($data);

 

print_r($do);

 

file_put_contents("results", $sorted_data);

 

?>

Link to comment
https://forums.phpfreaks.com/topic/230658-quicksort-help-please/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.