Questions about the execution process of PHP quick sort code

WBOY
Release: 2016-07-06 13:54:01
Original
1055 people have browsed it

Questions about the execution process of PHP quick sort code

The question is this:
Why do two arrays, array empty and array 0 => int 6, appear when printing $left here? It stands to reason that after the recursive call reaches the array array 0 => int 1, the return $arr on line 71 is directly executed. How come there are still two arrays after that?

The source code is as follows:

<code>$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));</code>
Copy after login
Copy after login

Reply content:

Questions about the execution process of PHP quick sort code

The question is this:
Why do two arrays, array empty and array 0 => int 6, appear when printing $left here? It stands to reason that after the recursive call reaches the array array 0 => int 1, the return $arr on line 71 is directly executed. How come there are still two arrays after that?

The source code is as follows:

<code>$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));</code>
Copy after login
Copy after login

What you dump is left. If the array is [1, 3, 3], it is empty, and [2, 3, 1] is one

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

<code>$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;
}</code>
Copy after login

}
aaa($arr,$result);

It feels simpler to use this idea

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!