1<?PHP2 functionQuickSort ($arr) {3 //determine if you need to proceed4 $length=Count($arr);5 if($length<= 1) {6 return $arr;7 }8 //If there is no return, the number of elements in the array is 1 extra and needs to be sorted9 //Select a rulerTen //Select the first element One $base _num=$arr[0]; A //iterates through all elements except the ruler, and fits into two arrays in relation to size - //initialization of two arrays - $left _array=Array();//less than the ruler the $right _array=Array();//larger 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 in the right array A $right _array[] =$arr[$i]; at } - } - //The same sort method for the left and right arrays, respectively. - //Recursively call this function and record the result - $left _array= QuickSort ($left _array); - $right _array= QuickSort ($right _array); in - //Merge left ruler right to return Array_merge($left _array,Array($base _num),$right _array); + } - $arr=Array(88, 1, 2, 5, 4, 3, 66, 0); the $res= QuickSort ($arr); * Print_r($res);
PHP Quick Sort Method