PHP Data structure and algorithm (PHP description) Quick Sort sort_php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/**
* Fast sorting 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; If only one array of data is returned directly

$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]; Less than put to the left
$len _l++;
} else {
$arrRight [$len _r] = $arrData [$i]; On the right with greater than or equal
$len _r++;
}
}

Merging arrays
$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, "==========================================
";
echo "Data:", Print_r ($arrData), "
";
echo "Filter Left:", Print_r ($arrLeft), "
";
echo "Filter Right:", Print_r ($arrRight), "
";
echo "return:", Print_r ($arrResult), "
";

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 "

http://www.bkjia.com/PHPjc/325534.html www.bkjia.com true http://www.bkjia.com/PHPjc/325534.html techarticle Copy the code as follows:? PHP/** * Fast sort Quick Sorts * **/function Sort_quick ($arrData) {if (empty ($arrData) | | |!is_array ($ARRDATA)) return false; $flag = $arrData [0]; $l ...

  • 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.