Merging sorted arrays

Source: Internet
Author: User

Merges two sorted integer arrays A and b into a new array.

Given a = [1, 2, 3, empty, empty] B = [4,5],

After merging a will become [1,2,3,4,5].

Where the array A has enough space, that is, if the number of non-empty elements of A is m,b the number of non-empty elements is n, then A's space capacity must be greater than or equal to M+n

1<?PHP2 /**3 * Merge two sorted integer arrays A and b into a new array (A has enough space (the size of a array is greater than or equal to m+n) to add elements in B)4 * A = [1, 2, 3, empty, empty], B = [4, 5]5 * After merging a will become [1, 2, 3, 4, 5]6  */7 8 /**9 * A = [1, 2, 3, empty, empty]Ten * B = [4, 5] One * m = 3 A * n = 2 - * m+n-1=4 - * So from a[4] This position, start assigning values forward the * A. Compare a[2]=3 and b[1], because a[2]<b[1], so a[4]=b[1]=5 - * b. Compare b[0] and a[2], the large assigns to a[3] - * C. Follow this rule to continue scanning until one scan is complete -  * + * One of the scans is done.  - * If a scan is finished first, you can imagine that this is the case: if a = [4, 5, 6, empty, empty]; b = [1, 2] will eventually become a = [4, 5, 4, 5, 6], so we need to assign the remainder of B [1, 2] to the part of a that is not yet assigned to a value + * If b scan is finished first, no action is required (a front is also ordered) A  */ at functionSolution ($a,$b) - { -     //default $ A, $b are not empty, no parameter checks -     $size=Count($a)-1;//the tail subscript of the array $ A -     $n=Count($b)-1; -     $m=$size-$n-1; in      -      while($n>=0 &&$m>=0) { to         if($a[$m] <$b[$n]) { +             $a[$size] =$b[$n]; -             $n--; the}Else { *             $a[$size] =$a[$m]; $             $m--;Panax Notoginseng         } -         $size--; the     } +      A     //If $b is finished first, there is no problem, but if $ A is first cycled, the problem will occur; the //Example: A = [4, 5, 6, empty, empty]; B = [1, 2] will eventually become a = [4, 5, 4, 5, 6] +      while($n>=0) { -         $a[$n] =$b[$n]; $         $n--; $     } -     return $a; - } the  - $a= [1, 2, 3, 0, 0];//0 means emptyWuyi $b= [4, 5]; the $res= Solution ($a,$b); - Echo"<pre>"; Wu Print_r($res); -  About $a= [4, 5, 6, 0, 0];//0 means empty $ $b= [1, 2]; - $res= Solution ($a,$b); - Echo"<pre>"; - Print_r($res);

Merging sorted arrays

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.