PHP Quick Sorting algorithm detailed, sorting algorithm detailed _php tutorial

Source: Internet
Author: User

PHP Quick Sorting algorithm detailed, sorting algorithm detailed


Concept

Here to borrow a map of Baidu Encyclopedia, very image:

The fast sorting algorithm is an optimization of the bubbling algorithm. His idea is to first split the array, put the large element values into a temporary array, the small element values into another temporary array (the split point can be an array of any element value, generally with the first element, that is, $array[0]), and then continue to repeat the two temporary array of the above split, Finally, the small array elements and the large array elements are merged together. The idea of recursion is used here.

PHP implementation

Copy the Code code as follows:
/*
Quick Sort
*/

function QuickSort ($array)
{
if (!isset ($array [1]))
return $array;
$mid = $array [0]; Gets a keyword for segmentation, typically the first element
$leftArray = Array ();
$rightArray = Array ();

foreach ($array as $v)
{
if ($v > $mid)
$rightArray [] = $v; Put a number larger than $mid into an array.
if ($v < $mid)
$leftArray [] = $v; Put a number smaller than $mid in another array
}

$leftArray = QuickSort ($leftArray); Split the smaller array once again
$leftArray [] = $mid; Add the split element to the small array, and don't forget it.

$rightArray = QuickSort ($rightArray); Split the larger array once again
Return Array_merge ($leftArray, $rightArray); Combination of two results
}

Comparison with bubbling algorithm

Here I compare with the previously written bubble algorithm implementation of the order, you can see that this algorithm than the bubble algorithm efficiency is much higher.

Copy the Code code as follows:
$a = Array_rand (range (1,3000), 1500); Even when the bubbling algorithm more than 1600 elements of the memory is not enough hints, but here in order to measure the difference between the size of two, it is set to 1500, to ensure that the bubbling algorithm can also be completed.
Shuffle ($a); Get an array that has been scrambled in order
$t 1 = microtime (true);
QuickSort ($a); Quick Sort
$t 2 = Microtime (true);
Echo (($t 2-$t 1) *1000). ' Ms
';

Require ('./maopao.php '); This is the sort of bubble algorithm I wrote before.
$t 1 = microtime (true);
Maopao ($a); Bubble
$t 2 = Microtime (true);
Echo (($t 2-$t 1) *1000). ' Ms ';

Operation Result:

Copy the Code code as follows:
12.10880279541ms
772.64094352722ms

http://www.bkjia.com/PHPjc/909348.html www.bkjia.com true http://www.bkjia.com/PHPjc/909348.html techarticle PHP Quick Sorting algorithm detailed, sorting algorithm detailed concept here to borrow a map of Baidu Encyclopedia, very image: Fast sorting algorithm is an optimization of the bubbling algorithm. His mind is ...

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