I want to implement the recursion under PHP, the following code can only achieve the first order,
But do not know how to implement recursion, understand by the two left and right array merge, or silly do not understand.
Ask you, help to optimize the code, or paste the results. Thank you ~
$TMPB) {echo $arr [$j], "than", $TMPB, "Big go On", "\ n"; -$j; Echo ' $j ', "Minus 1, subscript", $j, "value is-"; Echo $arr [$j]. " \ n "; Echo ' Now array is ', ' \ n '; Var_dump ($arr); }//If this value is smaller than pivot, then swap, then from start to left find if ($arr [$j]< $tmpb) {echo $arr [$j], "than", $TMPB, "small ~"; echo ' swap ', ' \ n '; Swap ($arr [$j], $arr [$i]); + + $i; Echo ' Now array is ', ' \ n '; Var_dump ($arr); Echo ' $i ', "plus 1, subscript", $i, "value is-"; Echo $arr [$i]. " \ n "; echo "starts from the left"; echo "\ n"; }//Then from the left to find while ($arr [$i]< $tmpb) {echo $arr [$i], ' than ', $TMPB, ' Little go on ', ' \ n ', '; + + $i; Echo ' $i ', "plus 1, subscript", $i, "value is---"; echo $arr [$i]. " \ n "; if ($arr [$i]> $tmpb) {echo $arr [$i], ' than ', $TMPB, ' big, swap '; Swap ($arr [$i], $arr [$j]); -$j; Echo ' $j ', "Minus 1, subscript", $j, "value is-"; Echo $arr [$j]. " \ n "; Echo ' Now array is ', ' \ n '; Var_dump($arr); echo "starts on the right side"; Echo ' Go another layer of the outermost while ', ' \ n '; }} return $arr;} $result =quicksort ($arr); echo "========================", "final result is \ n"; var_dump ($result);
The results of the execution I posted:
======================== The final result is
Array (
0 = 23,
1 = 13,
2 = 51,
3 = 57,
4 = 26,
5 = 66,
6 = 81,
7 = 69,
8 = 76,
He just pressed to find 66 divided into two parts, combined with my thoughts above, how can recursion?
Reply content:
I want to implement the recursion under PHP, the following code can only achieve the first order,
But do not know how to implement recursion, understand by the two left and right array merge, or silly do not understand.
Ask you, help to optimize the code, or paste the results. Thank you ~
$TMPB) {echo $arr [$j], "than", $TMPB, "Big go On", "\ n"; -$j; Echo ' $j ', "Minus 1, subscript", $j, "value is-"; Echo $arr [$j]. " \ n "; Echo ' Now array is ', ' \ n '; Var_dump ($arr); }//If this value is smaller than pivot, then swap, then from start to left find if ($arr [$j]< $tmpb) {echo $arr [$j], "than", $TMPB, "small ~"; echo ' swap ', ' \ n '; Swap ($arr [$j], $arr [$i]); + + $i; Echo ' Now array is ', ' \ n '; Var_dump ($arr); Echo ' $i ', "plus 1, subscript", $i, "value is-"; Echo $arr [$i]. " \ n "; echo "starts from the left"; echo "\ n"; }//Then from the left to find while ($arr [$i]< $tmpb) {echo $arr [$i], ' than ', $TMPB, ' Little go on ', ' \ n ', '; + + $i; Echo ' $i ', "plus 1, subscript", $i, "value is---"; echo $arr [$i]. " \ n "; if ($arr [$i]> $tmpb) {echo $arr [$i], ' than ', $TMPB, ' big, swap '; Swap ($arr [$i], $arr [$j]); -$j; Echo ' $j ', "Minus 1, subscript", $j, "value is-"; Echo $arr [$j]. " \ n "; Echo ' Now array is ', ' \ n '; Var_dump($arr); echo "starts on the right side"; Echo ' Go another layer of the outermost while ', ' \ n '; }} return $arr;} $result =quicksort ($arr); echo "========================", "final result is \ n"; var_dump ($result);
The results of the execution I posted:
======================== The final result is
Array (
0 = 23,
1 = 13,
2 = 51,
3 = 57,
4 = 26,
5 = 66,
6 = 81,
7 = 69,
8 = 76,
He just pressed to find 66 divided into two parts, combined with my thoughts above, how can recursion?
`
public static function quickSort( $numArr ){ $minArr = []; $maxArr = []; $randKey = array_rand( $numArr ); $baseNum = $numArr[$randKey]; unset( $numArr[$randKey] ); foreach ( $numArr as $num ) { if ( $num >= $baseNum ) $maxArr[] = $num; else $minArr[] = $num; } if ( count( $maxArr ) > 1 ) $maxArr = self::quickSort( $maxArr ); if ( count( $minArr ) > 1 ) $minArr = self::quickSort( $minArr ); return array_merge( $minArr, [$baseNum], $maxArr );}`
That's what I did.