白云岛资源网 Design By www.pvray.com
复制代码 代码如下:
<?php
/**
* 快速排序 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; // 如果只有一个数据的数组直接返回
$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]; // 小于的放左边
$len_l++;
} else {
$arrRight[$len_r] = $arrData[$i]; // 大于等于的放右边
$len_r++;
}
}
// 合并数组
$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," ==========================================<br/>";
echo "data : ",print_r($arrData),"<br/>";
echo "filter left: ",print_r($arrLeft),"<br/>";
echo "filter right: ",print_r($arrRight),"<br/>";
echo "return : ",print_r($arrResult),"<br/>";
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 "<pre>";print_r($list);
<?php
/**
* 快速排序 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; // 如果只有一个数据的数组直接返回
$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]; // 小于的放左边
$len_l++;
} else {
$arrRight[$len_r] = $arrData[$i]; // 大于等于的放右边
$len_r++;
}
}
// 合并数组
$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," ==========================================<br/>";
echo "data : ",print_r($arrData),"<br/>";
echo "filter left: ",print_r($arrLeft),"<br/>";
echo "filter right: ",print_r($arrRight),"<br/>";
echo "return : ",print_r($arrResult),"<br/>";
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 "<pre>";print_r($list);
白云岛资源网 Design By www.pvray.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
白云岛资源网 Design By www.pvray.com
暂无评论...