PHP Quick Sort algorithm detailed _php skill

Source: Internet
Author: User

Concept

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

The fast sorting algorithm is an optimization of the bubbling algorithm. His idea was to divide the array into groups, and to put the values of the large elements in a temporary one, place the small element value in another temporary array (the split point can be any of the element values in the array, usually with the first element, i.e. $array[0]), and then continue to split the two temporary arrays again. Finally, the small array elements are merged with the large array elements. The idea of recursion is used here.

PHP implementation

Copy 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 the number larger than the $mid into an array.
if ($v < $mid)
$leftArray [] = $v; Put a number smaller than $mid into another array.
}

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

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

Comparison with bubble algorithm

Here I compare the sequence of the bubbles algorithm that I wrote earlier to see that the algorithm is much more efficient than the bubbling algorithm.

Copy Code code as follows:

$a = Array_rand (range (1,3000), 1500); Even if the bubble algorithm is more than 1600 elements of the time there will be low memory prompts, but here to measure the difference between the size of two, is set to 1500, to ensure that the bubble algorithm can also be completed.
Shuffle ($a); Gets the array that has scrambled the order
$t 1 = microtime (true);
QuickSort ($a); Quick Sort
$t 2 = Microtime (true);
Echo ($t 2-$t 1) *1000). ' Ms<br/> ';

Require ('./maopao.php '); Here 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 ';

Run Result:

Copy Code code as follows:

12.10880279541ms
772.64094352722ms

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.