Php quick sorting code execution process

Source: Internet
Author: User
The problem is: why are there two arrays, arrayempty and array0 & amp; gt; int6, printed on $ left? It is reasonable to say that after recursively calling the array array0 & amp; gt; int1, the return $ arr of the 71 rows will be executed directly. how can we still have the next two arrays ?...

The problem is as follows:
Why is the array empty and array 0 => int 6 displayed in $ left? It is reasonable to say that after recursively calling the array 0 => int 1, the return $ arr of the 71 rows will be executed directly. how can we still have the next two arrays?

The source code is as follows:

$arr = array(6,3,8,6,4,2,9,5,1);function quick_sort($arr){    if(!is_array($arr)) return false;        $length = count($arr);    if($length <= 1) return $arr;    $left = $right = array();    for($i = 1;$i < $length;$i++){                                                        if($arr[$i] < $arr[0]){            $left[] = $arr[$i];                                     }else{            $right[] = $arr[$i];                                               }    }    var_dump($left);    $left = quick_sort($left);    $right = quick_sort($right);    return array_merge($left,array($arr[0]),$right);}print_r(quick_sort($arr));

Reply content:

The problem is as follows:
Why is the array empty and array 0 => int 6 displayed in $ left? It is reasonable to say that after recursively calling the array 0 => int 1, the return $ arr of the 71 rows will be executed directly. how can we still have the next two arrays?

The source code is as follows:

$arr = array(6,3,8,6,4,2,9,5,1);function quick_sort($arr){    if(!is_array($arr)) return false;        $length = count($arr);    if($length <= 1) return $arr;    $left = $right = array();    for($i = 1;$i < $length;$i++){                                                        if($arr[$i] < $arr[0]){            $left[] = $arr[$i];                                     }else{            $right[] = $arr[$i];                                               }    }    var_dump($left);    $left = quick_sort($left);    $right = quick_sort($right);    return array_merge($left,array($arr[0]),$right);}print_r(quick_sort($arr));

Dump is left. If the array is [, 3], it is null, and [, 1] is one.

$ Arr = array (6, 3, 8, 6, 4, 2, 9, 5, 1 );
$ Result = array ();
Function aaa ($ arr, $ result ){

$min = min($arr);array_push($result, $min);$key=array_search($min ,$arr);array_splice($arr,$key,1);if(count($arr)){    aaa($arr,$result);}else{    echo json_encode($result);    exit;}

}
Aaa ($ arr, $ result );

It is easier to use this idea.

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.