Recursive calls-how to get a quick sort of PHP, how to recursion?

Source: Internet
Author: User
Keywords PHP recursive invocation
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.