PHP data structure and algorithm (PHP description) quick sort quick sort_PHP tutorial

WBOY
Release: 2016-07-21 15:18:34
Original
893 people have browsed it

Copy code The code is as follows:

/**
* quick sort
*
**/

function sort_quick($arrData) {
if(empty($arrData) || !is_array($arrData)) return false;

$flag = $arrData[0];
$len = count($arrData) - 1;
if($len == 0) return $arrData; // If there is only one data array, return directly

$arrLeft = array();
$ arrRight = array();
$len_l = 0;
$len_r = 0;
for($i = 1; $i <= $len;$i++) {
if($ arrData[$i] < $flag) {
$arrLeft[$len_l] = $arrData[$i]; // Smaller ones are placed on the left
$len_l++;
} else {
$ arrRight[$len_r] = $arrData[$i]; // Place greater than or equal to the right
$len_r++;
}
}

// Merge arrays
$arrResult = array();
if($len_l) {
$arrLeft = sort_quick($arrLeft);
for($i = 0;$i <= $len_l - 1; $i++ ) {
$arrResult[$i] = $arrLeft[$i];
}
}
$arrResult[$len_l] = $flag;
$len_l++;
if($len_r ) {
$arrRight = sort_quick($arrRight);
for($i = 0;$i <= $len_r - 1; $i++ ) {
$arrResult[$len_l] = $arrRight [$i];
$len_l++;
}
}
echo "== ",$flag," ================== ========================
";
echo "data : ",print_r($arrData),"< br/>";
echo "filter left: ",print_r($arrLeft),"
";
echo "filter right: ",print_r($arrRight),"< br/>";
echo "return : ",print_r($arrResult),"
";

return $arrResult;
}
//$ list = array(4,3,2,1,5,7,3,7);
$list = array(4,51,6,73,2,5,9,33,50,3,4 ,6,1,4,67);
$list = sort_quick($list);
echo "
";print_r($list); 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325534.htmlTechArticleCopy the code as follows: ?php /*** Quick sort quick sort * **/ function sort_quick($arrData) { if( empty($arrData) || !is_array($arrData)) return false; $flag = $arrData[0]; $l...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template