Home > Backend Development > PHP Tutorial > php实现快速排序的有关问题

php实现快速排序的有关问题

WBOY
Release: 2016-06-13 12:56:16
Original
877 people have browsed it

php实现快速排序的问题?

<br />
function quick(&$arr,$low,$high){<br />
<br />
    $key = $low;<br />
    <br />
    for($i=$low,$j=$high;$i!=$j;){<br />
    <br />
       for(;$j>$key;){<br />
    <br />
           if($arr[$j]<$arr[$key]){<br />
<br />
               list($arr[$j],$arr[$key])=swap($arr[$j],$arr[$key]);     <br />
               $key = $j;<br />
               break;<br />
<br />
           }else{<br />
<br />
               $j--;<br />
<br />
           }<br />
    <br />
       }<br />
    <br />
       for(;$i<$key;){<br />
    <br />
           if($arr[$i]>$arr[$key]){<br />
<br />
               list($arr[$i],$arr[$key])=swap($arr[$i],$arr[$key]);     <br />
               $key = $i;<br />
               break;<br />
<br />
           }else{<br />
<br />
               $i++;<br />
<br />
           }<br />
    <br />
       }<br />
    }<br />
 <br />
    if($high>$low){ <br />
<br />
        quick($arr,$low,$key-1); <br />
        quick($arr,$key+1,$high);<br />
<br />
    }<br />
<br />
}<br />
<br />
$arr = array(7,4,9,1,3,2,6,8,0);<br />
quick($arr,0,8);<br />
print_r($arr);<br />
Copy after login


不递归的话单跑一遍正常(去掉if($high>$low)那一段)。一递归就不行了,Chrome转半天,应该是无限递归下去了。请问一下问题出在哪呢?

php 算法 快递排序
------解决方案--------------------
看得我眼冒金星
Related labels:
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