I have some problems with the output of the php quick sorting function.

Source: Internet
Author: User
{Code ...} output Array ([0] & amp; gt; [1] & amp; gt; [2] & amp; gt; 21 [3] & amp; gt; 333 [4] & amp; gt; 411 [5] & amp; gt; 666) Why are two more null values in front?
function qsort($arr){    if(!isset($arr[1])){        return $arr;    }    for ($i=0; $i < count($arr); $i++) {         if($arr[$i+1]<$arr[0]){            $larr[] = $arr[$i+1];        }        if($arr[$i+1]>=$arr[0]){            $rarr[] = $arr[$i+1];        }    }    $larr = qsort($larr);    $larr[] = $arr[0];    $rarr = qsort($rarr);    return array_merge($larr,$rarr);}print_r(qsort(array(411,21,333,666)));

Output Array ([0] => [1] => [2] => 21 [3] => 333 [4] => 411 [5] => 666)

Why is there two more null values in front?

Reply content:
function qsort($arr){    if(!isset($arr[1])){        return $arr;    }    for ($i=0; $i < count($arr); $i++) {         if($arr[$i+1]<$arr[0]){            $larr[] = $arr[$i+1];        }        if($arr[$i+1]>=$arr[0]){            $rarr[] = $arr[$i+1];        }    }    $larr = qsort($larr);    $larr[] = $arr[0];    $rarr = qsort($rarr);    return array_merge($larr,$rarr);}print_r(qsort(array(411,21,333,666)));

Output Array ([0] => [1] => [2] => 21 [3] => 333 [4] => 411 [5] => 666)

Why is there two more null values in front?

The end condition of the for loop is $ I.

Then you must turn off and report an error. Therefore, PHP does not report an error. Instead, it considers that the result is a null value and continues to be executed. Therefore, the sorting result contains a null value.

Correct:


  = $ Arr [0]) {$ rarr [] = $ arr [$ I] ;}}$ larr = qsort ($ larr ); $ larr [] = $ arr [0]; $ rarr = qsort ($ rarr); return array_merge ($ larr, $ rarr);} print_r (qsort (array (411,21, 333,666 )));

Https://3v4l.org/CIKED

    function querySort($arr,$order='asc') {        if (count($arr)<1) {            return $arr;        }        $arr_left = $arr_right = array();        $val = $arr[0]; unset($arr[0]);        foreach ($arr as $key => $value) {            if ($order=='desc') {                if ($value < $val) {                    $arr_left[] = $value;                } else {                    $arr_right[] = $value;                }            } else {                if ($value > $val) {                    $arr_left[] = $value;                } else {                    $arr_right[] = $value;                }            }        }        $arr_left = querySort($arr_left,$order);        $arr_right = querySort($arr_right,$order);        return array_merge($arr_left,[$val],$arr_right);    }    $arr = querySort(array(411,21,333,666));    echo "
";    var_dump($arr);

It should be the reason for array_merge

Why write and sort it like this? The built-in array sorting function can be used!

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.