这篇文章介绍的内容是关于快速排序递归版php实现,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
今天开始复习算法,连最熟悉的快排都写不出来了,汗颜,贴下代码,以备后用吧
function qSort(array &$a, $low, $high)
{
if($low >= $high) {
return;
}
$index = partition($a,$low,$high);
qSort($a,$low,$index-1);
qSort($a,$index+1,$high);
}//元素相互赋值比交换效率
function partition(array &$a, $low, $high)
{
$temp = $a[$low];
while($low < $high) {
while($low < $high && $a[$high] >= $temp) {
--$high;
}
$a[$low] = $a[$high];
while($low < $high && $a[$low] <= $temp) {
++$low;
}
$a[$high] = $a[$low];
}
$a[$low] = $temp;
return $low;
}$a = [0,20,7,-1,6,2,6,2,8,9,0,1];
qSort($a, 0, count($a) -1);
var_dump(implode(',', $a));
结果展示:-1,0,0,1,2,2,6,6,7,8,9,20
相关推荐:
立即学习“PHP免费学习笔记(深入)”;
以上就是快速排序递归版php实现的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号