PHP implementation of fast sorting method function code
Source: Internet
Author: User
Code Listing 1:
Copy the code code as follows:
<?php
function Quicksort ($STR) {
if (count ($str) <=1) return $str;//If the number is not greater than one, return directly
$key = $str [0];//takes a value, which is later used for comparison;
$left _arr=array ();
$right _arr=array ();
for ($i =1; $i <count ($STR), $i + +) {//is larger than $key on the right, small on the left;
if ($str [$i]<= $key)
$left _arr[]= $str [$i];
Else
$right _arr[]= $str [$i];
}
$left _arr=quicksort ($left _arr);//recursive;
$right _arr=quicksort ($right _arr);
Return Array_merge ($left _arr,array ($key), $right _arr);//Combine the values in the left and right into an array;
}//Below is the test
$str =array (5,3,8,2,5,9,7,2,1,4,0);
Print_r (Quicksort ($STR));
?>
Code Listing 2:
Copy the code code as follows:
/* Fast Sorting Method */
function QuickSort ($left, $right, $arr) {
$l = $left;
$r = $right;
$pivot = $arr [($left + $right)/2];
$temp = 0;
while ($l < $r) {
while ($arr [$l]< $pivot) {
$l + +;
}
while ($arr [$r]> $pivot) {
$r--;
}
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.