Quick Sort Recursive version PHP implementation

Source: Internet
Author: User
This article introduces the content of the quick sort recursive version of PHP implementation, has a certain reference value, now share to everyone, the need for friends can refer to

Today began to review the algorithm, even the most familiar with the fast line is not written out, shame, put the code, for later use it

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);}
The element is mutually assignable than the Exchange efficiency 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));

Results showing: -1,0,0,1,2,2,6,6,7,8,9,20

Related Article

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.