Sorted array:
Given two sorted integer arrays A and B, merge B intoAsOne sorted array. Note: You may assume that a has enough space (size thatIsGreater or equal to m + n) to hold additional elementsFromB. The number of elements initializedInA and B are M and N respectively.
Problem Analysis:
Merge Sorting
Note: you cannot apply for additional memory space for sorting results, which are directly stored in array.ShouldFrom the back to the frontFill in the result ArrayTo avoid overwriting the elements in the original array.
Class Solution { Public : Void Merge ( Int A [], Int M, Int B [], Int N) {assert ( ! = NULL & B! = NULL & M> = 0 & Amp; N & gt; = 0 ); Int IA = m- 1 ; Int IB = N- 1 ; Int Idx = m + n- 1 ; While (Ia> = 0 & IB> = 0 ){ If (A [Ia]> = B [IB]) {A [idx --] = A [IA -- ];} Else {A [idx --] = B [IB -- ] ;}} While (Ia> = 0 ) {A [idx --] = A [IA -- ];} While (Ib> = 0 ) {A [idx --] = B [IB -- ] ;}};