PHP Data structure and algorithm (PHP description) Fast sorting quick sort_php tips

Source: Internet
Author: User
Copy Code code as follows:

<?php
/**
* 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 left.
$len _l++;
} else {
$arrRight [$len _r] = $arrData [$i]; is greater than or equal to the right
$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, "==========================================<br/>";
echo "Data:", Print_r ($arrData), "<br/>";
echo "Filter Left:", Print_r ($arrLeft), "<br/>";
echo "Filter Right:", Print_r ($arrRight), "<br/>";
echo "return:", Print_r ($arrResult), "<br/>";

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 "<pre>";p Rint_r ($list);

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.