Problems related to php quick sorting

Source: Internet
Author: User
How does php implement fast sorting? Function & nbsp; quick (& amp; $ arr, $ low, $ high) {& nbsp; $ key & nbsp; $ low; & nbsp; for ($ I $ low, $ j $ high; $ I! How does php implement fast sorting?

function quick(&$arr,$low,$high){

$key = $low;

for($i=$low,$j=$high;$i!=$j;){

for(;$j>$key;){

if($arr[$j]<$arr[$key]){

list($arr[$j],$arr[$key])=swap($arr[$j],$arr[$key]);
$key = $j;
break;

}else{

$j--;

}

}

for(;$i<$key;){

if($arr[$i]>$arr[$key]){

list($arr[$i],$arr[$key])=swap($arr[$i],$arr[$key]);
$key = $i;
break;

}else{

$i++;

}

}
}

if($high>$low){

quick($arr,$low,$key-1);
quick($arr,$key+1,$high);

}

}

$arr = array(7,4,9,1,3,2,6,8,0);
quick($arr,0,8);
print_r($arr);


If it is not recursive, it is normal to run it once (remove the if ($ high> $ low) section ). A recursion won't work. Chrome has been turning around for half a day. it should be infinite recursion. Where is the problem?

Php algorithm express sorting


------ Solution --------------------
Seeing Venus in my eyes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.