Please explain the sorting

Source: Internet
Author: User
Please explain in detail the recursion process functional_merge ($ arrA, $ arrB) {code...} Please explain in detail the recursion Process
Function al_merge ($ arrA, $ arrB ){

    $arrC = array();    while(count($arrA)&&count($arrB)){        $arrC[]=$arrA['0']<$arrB['0']?array_shift($arrA):array_shift($arrB);    }        return array_merge($arrC,$arrA,$arrB);}function al_merge_sort($arr){    $len = count($arr);    if($len<=1)        return $arr;    $mid = intval($len/2);    $left_arr = array_slice($arr,0,$mid);    $right_arr = array_slice($arr,$mid);    $left_arr = al_merge_sort($left_arr);    $right_arr = al_merge_sort($right_arr);    $arr = al_merge($left_arr,$right_arr);    return $arr;}$arr=array(5,7,8,3);print_r(al_merge_sort($arr));

Reply content:

Please explain the recursion process in detail
Function al_merge ($ arrA, $ arrB ){

    $arrC = array();    while(count($arrA)&&count($arrB)){        $arrC[]=$arrA['0']<$arrB['0']?array_shift($arrA):array_shift($arrB);    }        return array_merge($arrC,$arrA,$arrB);}function al_merge_sort($arr){    $len = count($arr);    if($len<=1)        return $arr;    $mid = intval($len/2);    $left_arr = array_slice($arr,0,$mid);    $right_arr = array_slice($arr,$mid);    $left_arr = al_merge_sort($left_arr);    $right_arr = al_merge_sort($right_arr);    $arr = al_merge($left_arr,$right_arr);    return $arr;}$arr=array(5,7,8,3);print_r(al_merge_sort($arr));

Alas, you shouldn't be too impatient with downvote. Let's just say, you have three main problems:

  • If you see many problems in your profile, you only need to carefully debug them to solve them.
  • No good use of search engines. Programmers like to share their knowledge, so there are a lot of blogs on algorithms and libraries, No matter what syntax they explain.
  • It is best to ask this question in the sf sub-station 101. Speaking of sf, we recommend that you make good use of markdown layout.

Back to this question, this sort is called "merge sort", and the algorithm IDEA is "divide and conquer". You can find a lot of data by moving your fingers on the search engine.

  1. First$arrSplit into two Arrays$left_arr,$right_arr.
  2. Call these arrays again.al_merge_sort()In this process, the entire array is continuously split and then split.
  3. If this happens, one day the array will have only one element left. One element does not need to be sorted and will be directly returned.
  4. In this case, we wantal_merge_sortThe returned results are sorted arrays.
  5. Then callal_merge()So that it can be mixed up with two sorted arrays from small to large.
  6. In this way, a new sorted array is formed. Well, the returned result is. Repeat Step 4 after the returned result.
  7. The code is always at the top, that is, the last line of your code called. It is also a sorted array.

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.