Code 1:
Copy Code code as follows:
<?php
function Quicksort ($STR) {
if (count ($str) <=1) return $str;//If the number is not greater than one, go straight back
$key = $str [0];//takes a value and 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 left and right values 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 2:
Copy 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-;
}
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;
}
}