Code Listing 1:
Copy CodeThe code is as follows:
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 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 CodeThe code is 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--;
}
if ($l >= $r) break;
$temp = $arr [$l];
$arr [$l] = $arr [$r];
$arr [$r] = $temp;
if ($arr [$l]== $pivot)--$r;
if ($arr [$r]== $pivot) + + $l;
}
if ($l = = $r) {
$l + +;
$r--;
}
if ($left < $r) {
QuickSort ($left, $r, $arr);
}elseif ($right > $l) {
QuickSort ($l, $right, $arr);
}else{
return $arr;
}
}
http://www.bkjia.com/PHPjc/325921.html www.bkjia.com true http://www.bkjia.com/PHPjc/325921.html techarticle Code Listing 1: Copy code code as follows:? PHP function Quicksort ($str) {if ($STR) =1) return $str;//If the number is not greater than one, return directly $key = $str [0];//takes a value, Later used to ...