4. Quick Sort Method
Code:
function Quick_sort ($arr) {
Determine if you need to proceed
$length = count ($arr);
if ($length <= 1) {
return $arr;
}
If there is no return, the number of elements in the array is 1 extra and needs to be sorted
Select a ruler
Select the first element
$base _num = $arr [0];
Iterates through all elements except the ruler, and fits into two arrays in relation to size
Initialize two arrays
$left _array = Array ();//less than the ruler
$right _array = Array ();//greater than the ruler
for ($i =1; $i < $length; $i + +) {
if ($base _num > $arr [$i]) {
Put in the left array
$left _array[] = $arr [$i];
} else {
Put it on the right
$right _array[] = $arr [$i];
}
}
The same sort method for the left and right arrays, respectively.
Call this function recursively and record the result
$left _array = Quick_sort ($left _array);
$right _array = Quick_sort ($right _array);
Merge left ruler right
Return Array_merge ($left _array, Array ($base _num), $right _array);
}
PHP four basic algorithm----fast sorting method